-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
77 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {EditorView} from '@codemirror/view'; | ||
import {createAsyncAction} from '@core/hooks/async-action'; | ||
import {Resource} from 'solid-js'; | ||
import {exportImage, ExportImagePayload} from './use-export-image'; | ||
|
||
type ViewState = {printing: boolean}; | ||
|
||
type InternalEditorView = EditorView & { | ||
viewState?: ViewState; | ||
measure?(): void; | ||
}; | ||
|
||
export let previewEditorView!: EditorView; | ||
|
||
export function setPreviewEditorView(editorView: EditorView) { | ||
previewEditorView = editorView; | ||
} | ||
|
||
export function exportSnippet(options: ExportImagePayload) { | ||
const editorView = previewEditorView as InternalEditorView; | ||
if (editorView.viewState && editorView.measure) { | ||
// We need to set the viewState `printing` property to true in order to render the entire code block | ||
editorView.viewState.printing = true; | ||
// Then we measure again the editor in order to render every block | ||
editorView.measure(); | ||
} | ||
return exportImage(options).finally(() => { | ||
if (editorView.viewState) { | ||
// At the end of the render we need to put the printing property to false | ||
editorView.viewState.printing = false; | ||
} | ||
// Then we do a request measure since this event can be scheduled | ||
editorView.requestMeasure(); | ||
}); | ||
} | ||
|
||
export function useExportSnippet(): [ | ||
Resource<Blob | string | undefined>, | ||
(data: ExportImagePayload) => void, | ||
] { | ||
const [data, {notify}] = createAsyncAction( | ||
async (ref: ExportImagePayload) => { | ||
return exportSnippet(ref); | ||
}, | ||
); | ||
|
||
return [data, notify]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters