@widgetic/canvas 0.5.35 → 0.5.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -36
- package/dist/canvas/DomCanvas.svelte +449 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +221 -0
- package/dist/canvas/boardFrame.d.ts +13 -0
- package/dist/canvas/boardFrame.js +13 -0
- package/dist/canvas/boardSurface.d.ts +9 -0
- package/dist/canvas/boardSurface.js +9 -0
- package/dist/canvas/chromeLayout.d.ts +76 -0
- package/dist/canvas/chromeLayout.js +121 -0
- package/dist/canvas/emptyBoardHint.d.ts +17 -0
- package/dist/canvas/emptyBoardHint.js +12 -0
- package/dist/canvas/eventScope.d.ts +1 -1
- package/dist/canvas/eventScope.js +1 -1
- package/dist/canvas/host/CanvasHost.d.ts +348 -0
- package/dist/canvas/host/CanvasHost.js +1715 -0
- package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
- package/dist/canvas/host/CanvasHostApi.js +95 -0
- package/dist/canvas/host/browserServices.d.ts +46 -0
- package/dist/canvas/host/browserServices.js +167 -0
- package/dist/canvas/host/conversion.d.ts +53 -0
- package/dist/canvas/host/conversion.js +125 -0
- package/dist/canvas/host/hostObject.d.ts +40 -0
- package/dist/canvas/host/hostObject.js +93 -0
- package/dist/canvas/host/pageEvents.d.ts +24 -0
- package/dist/canvas/host/pageEvents.js +35 -0
- package/dist/canvas/host/screenRects.d.ts +66 -0
- package/dist/canvas/host/screenRects.js +102 -0
- package/dist/canvas/host/strokeSnap.d.ts +67 -0
- package/dist/canvas/host/strokeSnap.js +165 -0
- package/dist/canvas/index.d.ts +7 -7
- package/dist/canvas/index.js +8 -8
- package/dist/canvas/openMarks.d.ts +17 -0
- package/dist/canvas/openMarks.js +26 -0
- package/dist/canvas/panZoomPosition.d.ts +15 -0
- package/dist/canvas/panZoomPosition.js +16 -0
- package/dist/canvas/preferredTextFont.d.ts +12 -0
- package/dist/canvas/preferredTextFont.js +38 -0
- package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
- package/dist/canvas/shapes/ShapeLibrary.js +13 -1
- package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
- package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
- package/dist/canvas/toolbarPosition.d.ts +11 -11
- package/dist/canvas/toolbarPosition.js +11 -24
- package/dist/canvas/types.d.ts +14 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
- package/dist/canvas/ui/CanvasHint.svelte +21 -0
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
- package/dist/canvas/ui/ColorField.svelte +156 -0
- package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
- package/dist/canvas/ui/LoadNotice.svelte +39 -0
- package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
- package/dist/canvas/ui/SelectionPanel.svelte +300 -0
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
- package/dist/canvas/ui/SelectorBar.svelte +82 -0
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
- package/dist/canvas/ui/ShapesMenu.svelte +185 -0
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/ToolButton.svelte +78 -0
- package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
- package/dist/canvas/ui/WidgetActions.svelte +145 -0
- package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
- package/dist/canvas/ui/ZoomControl.svelte +89 -0
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
- package/dist/canvas/ui/colorModel.d.ts +18 -0
- package/dist/canvas/ui/colorModel.js +85 -0
- package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
- package/dist/canvas/ui/colorPickerMessages.js +15 -0
- package/dist/canvas/ui/floatingCard.d.ts +107 -0
- package/dist/canvas/ui/floatingCard.js +107 -0
- package/dist/canvas/ui/floatingLayer.d.ts +21 -0
- package/dist/canvas/ui/floatingLayer.js +52 -0
- package/dist/canvas/ui/icons.d.ts +12 -0
- package/dist/canvas/ui/icons.js +7 -0
- package/dist/canvas/ui/keyHints.d.ts +27 -0
- package/dist/canvas/ui/keyHints.js +43 -0
- package/dist/canvas/ui/popoverSide.d.ts +5 -0
- package/dist/canvas/ui/popoverSide.js +20 -0
- package/dist/canvas/ui/selectionTraits.d.ts +61 -0
- package/dist/canvas/ui/selectionTraits.js +99 -0
- package/dist/canvas/view/CanvasStage.svelte +132 -0
- package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
- package/dist/canvas/view/EraserTrail.svelte +50 -0
- package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
- package/dist/canvas/view/ObjectView.svelte +245 -0
- package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
- package/dist/canvas/view/SelectionOverlay.svelte +487 -0
- package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
- package/dist/canvas/view/TextEditor.svelte +76 -0
- package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.js +34 -0
- package/dist/canvas/view/geometry.d.ts +169 -0
- package/dist/canvas/view/geometry.js +272 -0
- package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
- package/dist/components/index.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +2 -2
- package/dist/engine/EngineCore.d.ts +273 -0
- package/dist/engine/EngineCore.js +1051 -0
- package/dist/engine/codec/canvasDocument.d.ts +57 -0
- package/dist/engine/codec/canvasDocument.js +471 -0
- package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
- package/dist/engine/codec/hostReadableKeys.js +25 -0
- package/dist/engine/core/arrange.d.ts +20 -0
- package/dist/engine/core/arrange.js +81 -0
- package/dist/engine/core/arrow.d.ts +91 -0
- package/dist/engine/core/arrow.js +182 -0
- package/dist/engine/core/brush.d.ts +68 -0
- package/dist/engine/core/brush.js +173 -0
- package/dist/engine/core/defaults.d.ts +70 -0
- package/dist/engine/core/defaults.js +158 -0
- package/dist/engine/core/freehand.d.ts +130 -0
- package/dist/engine/core/freehand.js +430 -0
- package/dist/engine/core/geometry.d.ts +44 -0
- package/dist/engine/core/geometry.js +216 -0
- package/dist/engine/core/history.d.ts +39 -0
- package/dist/engine/core/history.js +69 -0
- package/dist/engine/core/hitTest.d.ts +54 -0
- package/dist/engine/core/hitTest.js +298 -0
- package/dist/engine/core/matrix.d.ts +47 -0
- package/dist/engine/core/matrix.js +101 -0
- package/dist/engine/core/pathData.d.ts +50 -0
- package/dist/engine/core/pathData.js +550 -0
- package/dist/engine/core/rotationReset.d.ts +10 -0
- package/dist/engine/core/rotationReset.js +17 -0
- package/dist/engine/core/scene.d.ts +99 -0
- package/dist/engine/core/scene.js +450 -0
- package/dist/engine/core/scribble.d.ts +64 -0
- package/dist/engine/core/scribble.js +121 -0
- package/dist/engine/core/shapePaths.d.ts +34 -0
- package/dist/engine/core/shapePaths.js +69 -0
- package/dist/engine/core/shapeTools.d.ts +80 -0
- package/dist/engine/core/shapeTools.js +161 -0
- package/dist/engine/core/snapping.d.ts +64 -0
- package/dist/engine/core/snapping.js +164 -0
- package/dist/engine/core/style.d.ts +40 -0
- package/dist/engine/core/style.js +151 -0
- package/dist/engine/core/transform.d.ts +79 -0
- package/dist/engine/core/transform.js +258 -0
- package/dist/engine/core/types.d.ts +238 -0
- package/dist/engine/core/types.js +31 -0
- package/dist/engine/core/viewport.d.ts +32 -0
- package/dist/engine/core/viewport.js +84 -0
- package/dist/engine/input/PointerController.d.ts +38 -0
- package/dist/engine/input/PointerController.js +545 -0
- package/dist/engine/input/arrowHandles.d.ts +38 -0
- package/dist/engine/input/arrowHandles.js +60 -0
- package/dist/engine/input/gestures.d.ts +79 -0
- package/dist/engine/input/gestures.js +769 -0
- package/dist/engine/input/hoverCursor.d.ts +29 -0
- package/dist/engine/input/hoverCursor.js +23 -0
- package/dist/engine/input/keyboard.d.ts +75 -0
- package/dist/engine/input/keyboard.js +264 -0
- package/dist/engine/input/selectionHandles.d.ts +89 -0
- package/dist/engine/input/selectionHandles.js +169 -0
- package/dist/engine/input/types.d.ts +82 -0
- package/dist/engine/input/types.js +5 -0
- package/dist/engine/render/rasterize.d.ts +74 -0
- package/dist/engine/render/rasterize.js +273 -0
- package/dist/engine/state.d.ts +124 -0
- package/dist/engine/state.js +47 -0
- package/dist/engine/text/layoutText.d.ts +53 -0
- package/dist/engine/text/layoutText.js +170 -0
- package/dist/engine/widgets.d.ts +124 -0
- package/dist/engine/widgets.js +345 -0
- package/dist/i18n/labels.d.ts +4 -15
- package/dist/i18n/labels.js +33 -53
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +52 -124
- package/dist/i18n/messages.js +59 -133
- package/dist/i18n/shortcuts.d.ts +0 -1
- package/dist/i18n/shortcuts.js +2 -11
- package/dist/index.d.ts +4 -2
- package/dist/index.js +5 -3
- package/package.json +30 -5
- package/dist/canvas/Canvas.svelte +0 -13363
- package/dist/canvas/Canvas.svelte.d.ts +0 -286
- package/dist/canvas/CanvasToolbar.svelte +0 -1498
- package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
- package/dist/canvas/ContextMenu.svelte +0 -305
- package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
- package/dist/canvas/PanZoomPanel.svelte +0 -478
- package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
- package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
- package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
- package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
- package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
- package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
- package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
- package/dist/canvas/shadowRootText.d.ts +0 -5
- package/dist/canvas/shadowRootText.js +0 -43
- package/dist/canvas/shapes/FrameShape.d.ts +0 -97
- package/dist/canvas/shapes/FrameShape.js +0 -953
- package/dist/canvas/shapes/ImageShape.d.ts +0 -47
- package/dist/canvas/shapes/ImageShape.js +0 -339
- package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
- package/dist/canvas/shapes/WidgetShape.js +0 -200
- package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
- package/dist/canvas/widgetPreviewGeometry.js +0 -66
- package/dist/i18n/shapeWords.d.ts +0 -30
- package/dist/i18n/shapeWords.js +0 -36
- package/dist/i18n/slots.d.ts +0 -20
- package/dist/i18n/slots.js +0 -40
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local widget pixels -> CSS viewport pixels (no selection padding/stroke).
|
|
3
|
-
* Fabric matrices include rotation, skew, flips and owning groups. CSS starts
|
|
4
|
-
* at the top-left, while Fabric transforms around the object's center.
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* @param {import('fabric').FabricObject} object
|
|
8
|
-
* @param {import('fabric').Canvas} canvas
|
|
9
|
-
* @param {HTMLCanvasElement} canvasElement
|
|
10
|
-
*/
|
|
11
|
-
export function getWidgetPreviewGeometry(object, canvas, canvasElement) {
|
|
12
|
-
if (!canvas || !canvasElement || !object) return null;
|
|
13
|
-
const bounds = canvasElement.getBoundingClientRect();
|
|
14
|
-
const canvasWidth = canvas.getWidth() || bounds.width || 1;
|
|
15
|
-
const canvasHeight = canvas.getHeight() || bounds.height || 1;
|
|
16
|
-
const cssScaleX = bounds.width / canvasWidth;
|
|
17
|
-
const cssScaleY = bounds.height / canvasHeight;
|
|
18
|
-
const viewport = canvas.viewportTransform || [1, 0, 0, 1, 0, 0];
|
|
19
|
-
// calcACoords() is relative to the parent. Inside a multi-selection that
|
|
20
|
-
// parent is the selection, so those corners are not where the shape is
|
|
21
|
-
// painted. calcTransformMatrix() includes the selection.
|
|
22
|
-
const sceneMatrix = typeof object.calcTransformMatrix === 'function'
|
|
23
|
-
? object.calcTransformMatrix()
|
|
24
|
-
: null;
|
|
25
|
-
if (!sceneMatrix) return null;
|
|
26
|
-
// Local half-size only. calcTransformMatrix already applies scaleX/scaleY,
|
|
27
|
-
// so feeding it the scaled size makes the iframe lag the selection stroke
|
|
28
|
-
// and finish smaller or larger than the shape.
|
|
29
|
-
const halfWidth = (object.width || 1) / 2;
|
|
30
|
-
const halfHeight = (object.height || 1) / 2;
|
|
31
|
-
/** @param {number} x @param {number} y */
|
|
32
|
-
const scenePoint = (x, y) => ({
|
|
33
|
-
x: sceneMatrix[0] * x + sceneMatrix[2] * y + sceneMatrix[4],
|
|
34
|
-
y: sceneMatrix[1] * x + sceneMatrix[3] * y + sceneMatrix[5],
|
|
35
|
-
});
|
|
36
|
-
/** @param {{x: number, y: number}} point */
|
|
37
|
-
const project = (point) => ({
|
|
38
|
-
x: bounds.left + (viewport[0] * point.x + viewport[2] * point.y + viewport[4]) * cssScaleX,
|
|
39
|
-
y: bounds.top + (viewport[1] * point.x + viewport[3] * point.y + viewport[5]) * cssScaleY,
|
|
40
|
-
});
|
|
41
|
-
const tl = project(scenePoint(-halfWidth, -halfHeight));
|
|
42
|
-
const tr = project(scenePoint(halfWidth, -halfHeight));
|
|
43
|
-
const bl = project(scenePoint(-halfWidth, halfHeight));
|
|
44
|
-
const br = project(scenePoint(halfWidth, halfHeight));
|
|
45
|
-
const xs = [tl.x, tr.x, bl.x, br.x];
|
|
46
|
-
const ys = [tl.y, tr.y, bl.y, br.y];
|
|
47
|
-
const left = Math.min(...xs);
|
|
48
|
-
const top = Math.min(...ys);
|
|
49
|
-
const localWidth = object.width || 1;
|
|
50
|
-
const localHeight = object.height || 1;
|
|
51
|
-
const matrix = [
|
|
52
|
-
(tr.x - tl.x) / localWidth,
|
|
53
|
-
(tr.y - tl.y) / localWidth,
|
|
54
|
-
(bl.x - tl.x) / localHeight,
|
|
55
|
-
(bl.y - tl.y) / localHeight,
|
|
56
|
-
tl.x,
|
|
57
|
-
tl.y,
|
|
58
|
-
];
|
|
59
|
-
if (!matrix.every(Number.isFinite)) return null;
|
|
60
|
-
return {
|
|
61
|
-
left, top,
|
|
62
|
-
width: Math.max(...xs) - left,
|
|
63
|
-
height: Math.max(...ys) - top,
|
|
64
|
-
localWidth, localHeight, matrix,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { Translate } from '@widgetic/design-system/i18n';
|
|
2
|
-
import type { CanvasMessageKey } from './messages.js';
|
|
3
|
-
/**
|
|
4
|
-
* The words that are drawn onto the canvas itself: on an empty image or video, on a widget that has no picture yet, and
|
|
5
|
-
* above a frame that has no name. They are not part of any component, they are painted by the shapes (`ImageShape`,
|
|
6
|
-
* `WidgetShape`, `FrameShape`) every time the canvas redraws, so a shape reads them from the canvas it is on.
|
|
7
|
-
*/
|
|
8
|
-
export interface ShapeWords {
|
|
9
|
-
dropImage: string;
|
|
10
|
-
dropVideo: string;
|
|
11
|
-
or: string;
|
|
12
|
-
doubleClick: string;
|
|
13
|
-
/** What a widget on the canvas is called until it has a name. */
|
|
14
|
-
widgetName: string;
|
|
15
|
-
/** What a widget with no link says in place of its name. */
|
|
16
|
-
unlinkedWidget: string;
|
|
17
|
-
widgetPreview: string;
|
|
18
|
-
removeFromCanvas: string;
|
|
19
|
-
/** What is written above a frame that has no name. */
|
|
20
|
-
frameLabel: string;
|
|
21
|
-
}
|
|
22
|
-
/** The words, in the language `t` speaks. */
|
|
23
|
-
export declare function makeShapeWords(t: Translate<CanvasMessageKey>): ShapeWords;
|
|
24
|
-
/** Called by the canvas when it is made, and again when the page sets the language. */
|
|
25
|
-
export declare function setShapeWords(owner: object, words: ShapeWords): void;
|
|
26
|
-
/**
|
|
27
|
-
* The words for the shapes of one canvas. A shape that is not on a canvas (it is being made, or drawn to an image of its
|
|
28
|
-
* own) speaks English.
|
|
29
|
-
*/
|
|
30
|
-
export declare function shapeWordsOf(owner: object | null | undefined): ShapeWords;
|
package/dist/i18n/shapeWords.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { translateEnglish } from './language.js';
|
|
2
|
-
/** The words, in the language `t` speaks. */
|
|
3
|
-
export function makeShapeWords(t) {
|
|
4
|
-
return {
|
|
5
|
-
dropImage: t('canvas.draw.dropImage'),
|
|
6
|
-
dropVideo: t('canvas.draw.dropVideo'),
|
|
7
|
-
or: t('canvas.draw.or'),
|
|
8
|
-
doubleClick: t('canvas.draw.doubleClick'),
|
|
9
|
-
widgetName: t('canvas.draw.widgetName'),
|
|
10
|
-
unlinkedWidget: t('canvas.widget.unlinkedName'),
|
|
11
|
-
widgetPreview: t('canvas.draw.widgetPreview'),
|
|
12
|
-
removeFromCanvas: t('canvas.widget.removeFromCanvas'),
|
|
13
|
-
frameLabel: t('canvas.defaults.frameLabel')
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
// One set of words for each canvas: two canvases on a page can speak two languages. The key is the Fabric canvas the
|
|
17
|
-
// shapes are on, so the words go away with it.
|
|
18
|
-
const wordsOfCanvas = new WeakMap();
|
|
19
|
-
let englishWords = null;
|
|
20
|
-
/** Called by the canvas when it is made, and again when the page sets the language. */
|
|
21
|
-
export function setShapeWords(owner, words) {
|
|
22
|
-
wordsOfCanvas.set(owner, words);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* The words for the shapes of one canvas. A shape that is not on a canvas (it is being made, or drawn to an image of its
|
|
26
|
-
* own) speaks English.
|
|
27
|
-
*/
|
|
28
|
-
export function shapeWordsOf(owner) {
|
|
29
|
-
if (owner) {
|
|
30
|
-
const words = wordsOfCanvas.get(owner);
|
|
31
|
-
if (words)
|
|
32
|
-
return words;
|
|
33
|
-
}
|
|
34
|
-
englishWords ??= makeShapeWords(translateEnglish);
|
|
35
|
-
return englishWords;
|
|
36
|
-
}
|
package/dist/i18n/slots.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { Translate } from '@widgetic/design-system/i18n';
|
|
2
|
-
import type { CanvasMessageKey } from './messages.js';
|
|
3
|
-
/** A piece of a sentence that has markup inside it: plain text, or the place of one of its named parts. */
|
|
4
|
-
export type SentencePart = {
|
|
5
|
-
text: string;
|
|
6
|
-
} | {
|
|
7
|
-
slot: string;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Cuts a translated sentence into plain text and the places of its named parts. For a sentence that has markup inside
|
|
11
|
-
* it, such as a key shown as a key cap: the page's translation says where each part goes (`{key}`), so a language can
|
|
12
|
-
* put the parts in the order it needs, and the screen draws each part itself.
|
|
13
|
-
*
|
|
14
|
-
* sentenceParts('canvas.shortcuts.footer', ['key', 'hint', 'icon'], t)
|
|
15
|
-
* // → [{ text: 'Press ' }, { slot: 'key' }, { text: ' ' }, { slot: 'hint' }, { text: ' or click ' }, …]
|
|
16
|
-
*
|
|
17
|
-
* A part the translation leaves out is simply not in the list; a `{name}` that is not one of `slotNames` is shown as it
|
|
18
|
-
* is written, like any other message.
|
|
19
|
-
*/
|
|
20
|
-
export declare function sentenceParts(key: CanvasMessageKey, slotNames: readonly string[], t: Translate<CanvasMessageKey>): SentencePart[];
|
package/dist/i18n/slots.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// Two characters a translator never types (private use): they mark where a slot starts and ends, so the finished sentence
|
|
2
|
-
// can be cut there again. Two different marks, so a stray one in a translation cannot swallow the slot next to it.
|
|
3
|
-
const SLOT_OPEN = '\uE000';
|
|
4
|
-
const SLOT_CLOSE = '\uE001';
|
|
5
|
-
/**
|
|
6
|
-
* Cuts a translated sentence into plain text and the places of its named parts. For a sentence that has markup inside
|
|
7
|
-
* it, such as a key shown as a key cap: the page's translation says where each part goes (`{key}`), so a language can
|
|
8
|
-
* put the parts in the order it needs, and the screen draws each part itself.
|
|
9
|
-
*
|
|
10
|
-
* sentenceParts('canvas.shortcuts.footer', ['key', 'hint', 'icon'], t)
|
|
11
|
-
* // → [{ text: 'Press ' }, { slot: 'key' }, { text: ' ' }, { slot: 'hint' }, { text: ' or click ' }, …]
|
|
12
|
-
*
|
|
13
|
-
* A part the translation leaves out is simply not in the list; a `{name}` that is not one of `slotNames` is shown as it
|
|
14
|
-
* is written, like any other message.
|
|
15
|
-
*/
|
|
16
|
-
export function sentenceParts(key, slotNames, t) {
|
|
17
|
-
const values = {};
|
|
18
|
-
for (const name of slotNames)
|
|
19
|
-
values[name] = `${SLOT_OPEN}${name}${SLOT_CLOSE}`;
|
|
20
|
-
const sentence = t(key, values);
|
|
21
|
-
const parts = [];
|
|
22
|
-
const slotPattern = new RegExp(`${SLOT_OPEN}([^${SLOT_OPEN}${SLOT_CLOSE}]*)${SLOT_CLOSE}`, 'g');
|
|
23
|
-
let textFrom = 0;
|
|
24
|
-
for (const found of sentence.matchAll(slotPattern)) {
|
|
25
|
-
// Something between the two marks that is not one of our parts was typed by the translator: it stays text
|
|
26
|
-
if (!slotNames.includes(found[1]))
|
|
27
|
-
continue;
|
|
28
|
-
const at = found.index ?? 0;
|
|
29
|
-
if (at > textFrom)
|
|
30
|
-
parts.push({ text: sentence.slice(textFrom, at) });
|
|
31
|
-
parts.push({ slot: found[1] });
|
|
32
|
-
textFrom = at + found[0].length;
|
|
33
|
-
}
|
|
34
|
-
if (textFrom < sentence.length)
|
|
35
|
-
parts.push({ text: sentence.slice(textFrom) });
|
|
36
|
-
// A mark that was somehow in the translation itself is not part of any text (and a text that was only that is no text)
|
|
37
|
-
return parts
|
|
38
|
-
.map((part) => ('text' in part ? { text: part.text.split(SLOT_OPEN).join('').split(SLOT_CLOSE).join('') } : part))
|
|
39
|
-
.filter((part) => !('text' in part) || part.text !== '');
|
|
40
|
-
}
|