-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: intellij webview ReplicationStoreWorker
- Loading branch information
Showing
5 changed files
with
113 additions
and
4 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
36 changes: 36 additions & 0 deletions
36
packages/erd-editor-intellij-webview/src/services/replicationStore.worker.ts
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,36 @@ | ||
import { createReplicationStore } from '@dineug/erd-editor/engine.js'; | ||
import { | ||
AnyAction, | ||
Emitter, | ||
vscodeSaveValueAction, | ||
} from '@dineug/erd-editor-vscode-bridge'; | ||
|
||
import { toWidth } from '@/utils/text'; | ||
|
||
const store = createReplicationStore({ toWidth }); | ||
const bridge = new Emitter(); | ||
|
||
const dispatch = (action: AnyAction) => { | ||
globalThis.postMessage(action); | ||
}; | ||
|
||
store.on({ | ||
change: () => { | ||
dispatch( | ||
vscodeSaveValueAction({ | ||
value: store.value, | ||
}) | ||
); | ||
}, | ||
}); | ||
|
||
bridge.on({ | ||
webviewInitialValue: ({ payload: { value } }) => { | ||
store.setInitialValue(value); | ||
}, | ||
webviewReplication: ({ payload: { actions } }) => { | ||
store.dispatch(actions); | ||
}, | ||
}); | ||
|
||
globalThis.addEventListener('message', event => bridge.emit(event.data)); |
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,42 @@ | ||
const TEXT_PADDING = 2; | ||
const TextFontFamily = | ||
"-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', 'Open Sans', system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'" as const; | ||
|
||
let canvas: OffscreenCanvas | null = null; | ||
let canvasContext: OffscreenCanvasRenderingContext2D | null = null; | ||
|
||
function getCanvas(): OffscreenCanvas | null { | ||
if (canvas) { | ||
return canvas; | ||
} | ||
|
||
try { | ||
canvas = new OffscreenCanvas(0, 0); | ||
} catch {} | ||
|
||
return canvas; | ||
} | ||
|
||
function getCanvasContext() { | ||
if (canvasContext) { | ||
return canvasContext; | ||
} | ||
|
||
const canvas = getCanvas(); | ||
if (!canvas) return null; | ||
|
||
canvasContext = canvas.getContext('2d'); | ||
if (canvasContext) { | ||
canvasContext.font = `400 12px ${TextFontFamily}`; | ||
} | ||
return canvasContext; | ||
} | ||
|
||
export function toWidth(text: string) { | ||
const canvasContext = getCanvasContext(); | ||
const width = canvasContext | ||
? canvasContext.measureText(text).width | ||
: text.length * 10; | ||
|
||
return Math.round(width) + TEXT_PADDING; | ||
} |
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