Download frontend/src/state-providers/document.ts from openfree/graphite2: direct link, hf CLI and curl.
- Browser
- Download file 3.3 kB
-
https://huggingface.co/spaces/openfree/graphite2/resolve/main/frontend/src/state-providers/document.ts
- Command line
-
hf download hf://spaces/openfree/graphite2/frontend/src/state-providers/document.ts
-
curl -L -o document.ts https://huggingface.co/spaces/openfree/graphite2/resolve/main/frontend/src/state-providers/document.ts
3.3 kB
| import { tick } from "svelte"; | |
| import { writable } from "svelte/store"; | |
| import { type Editor } from "@graphite/editor"; | |
| import { | |
| defaultWidgetLayout, | |
| patchWidgetLayout, | |
| UpdateDocumentBarLayout, | |
| UpdateDocumentModeLayout, | |
| UpdateToolOptionsLayout, | |
| UpdateToolShelfLayout, | |
| UpdateWorkingColorsLayout, | |
| UpdateNodeGraphControlBarLayout, | |
| UpdateGraphViewOverlay, | |
| TriggerDelayedZoomCanvasToFitAll, | |
| UpdateGraphFadeArtwork, | |
| } from "@graphite/messages"; | |
| // eslint-disable-next-line @typescript-eslint/explicit-function-return-type | |
| export function createDocumentState(editor: Editor) { | |
| const state = writable({ | |
| // Layouts | |
| documentModeLayout: defaultWidgetLayout(), | |
| toolOptionsLayout: defaultWidgetLayout(), | |
| documentBarLayout: defaultWidgetLayout(), | |
| toolShelfLayout: defaultWidgetLayout(), | |
| workingColorsLayout: defaultWidgetLayout(), | |
| nodeGraphControlBarLayout: defaultWidgetLayout(), | |
| // Graph view overlay | |
| graphViewOverlayOpen: false, | |
| fadeArtwork: 100, | |
| }); | |
| const { subscribe, update } = state; | |
| // Update layouts | |
| editor.subscriptions.subscribeJsMessage(UpdateGraphFadeArtwork, (updateGraphFadeArtwork) => { | |
| update((state) => { | |
| state.fadeArtwork = updateGraphFadeArtwork.percentage; | |
| return state; | |
| }); | |
| }); | |
| editor.subscriptions.subscribeJsMessage(UpdateDocumentModeLayout, async (updateDocumentModeLayout) => { | |
| await tick(); | |
| update((state) => { | |
| patchWidgetLayout(state.documentModeLayout, updateDocumentModeLayout); | |
| return state; | |
| }); | |
| }); | |
| editor.subscriptions.subscribeJsMessage(UpdateToolOptionsLayout, async (updateToolOptionsLayout) => { | |
| await tick(); | |
| update((state) => { | |
| patchWidgetLayout(state.toolOptionsLayout, updateToolOptionsLayout); | |
| return state; | |
| }); | |
| }); | |
| editor.subscriptions.subscribeJsMessage(UpdateDocumentBarLayout, async (updateDocumentBarLayout) => { | |
| await tick(); | |
| update((state) => { | |
| patchWidgetLayout(state.documentBarLayout, updateDocumentBarLayout); | |
| return state; | |
| }); | |
| }); | |
| editor.subscriptions.subscribeJsMessage(UpdateToolShelfLayout, async (updateToolShelfLayout) => { | |
| await tick(); | |
| update((state) => { | |
| patchWidgetLayout(state.toolShelfLayout, updateToolShelfLayout); | |
| return state; | |
| }); | |
| }); | |
| editor.subscriptions.subscribeJsMessage(UpdateWorkingColorsLayout, async (updateWorkingColorsLayout) => { | |
| await tick(); | |
| update((state) => { | |
| patchWidgetLayout(state.workingColorsLayout, updateWorkingColorsLayout); | |
| return state; | |
| }); | |
| }); | |
| editor.subscriptions.subscribeJsMessage(UpdateNodeGraphControlBarLayout, (updateNodeGraphControlBarLayout) => { | |
| update((state) => { | |
| patchWidgetLayout(state.nodeGraphControlBarLayout, updateNodeGraphControlBarLayout); | |
| return state; | |
| }); | |
| }); | |
| // Show or hide the graph view overlay | |
| editor.subscriptions.subscribeJsMessage(UpdateGraphViewOverlay, (updateGraphViewOverlay) => { | |
| update((state) => { | |
| state.graphViewOverlayOpen = updateGraphViewOverlay.open; | |
| return state; | |
| }); | |
| }); | |
| editor.subscriptions.subscribeJsMessage(TriggerDelayedZoomCanvasToFitAll, () => { | |
| // TODO: This is horribly hacky | |
| [0, 1, 10, 50, 100, 200, 300, 400, 500].forEach((delay) => { | |
| setTimeout(() => editor.handle.zoomCanvasToFitAll(), delay); | |
| }); | |
| }); | |
| return { | |
| subscribe, | |
| }; | |
| } | |
| export type DocumentState = ReturnType<typeof createDocumentState>; | |