@mlightcad/mtext-input-box 0.1.0
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 +149 -0
- package/dist/controller/adapter.d.ts +13 -0
- package/dist/controller/adapter.js +119 -0
- package/dist/controller/index.d.ts +2 -0
- package/dist/controller/index.js +1 -0
- package/dist/controller/types.d.ts +46 -0
- package/dist/controller/types.js +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -0
- package/dist/model/ast.d.ts +13 -0
- package/dist/model/ast.js +214 -0
- package/dist/model/document.d.ts +31 -0
- package/dist/model/document.js +146 -0
- package/dist/model/index.d.ts +4 -0
- package/dist/model/index.js +3 -0
- package/dist/model/style.d.ts +6 -0
- package/dist/model/style.js +186 -0
- package/dist/model/types.d.ts +57 -0
- package/dist/model/types.js +1 -0
- package/dist/ui/icons.d.ts +11 -0
- package/dist/ui/icons.js +10 -0
- package/dist/ui/index.d.ts +2 -0
- package/dist/ui/index.js +2 -0
- package/dist/ui/toolbar.d.ts +34 -0
- package/dist/ui/toolbar.js +369 -0
- package/dist/viewer/format.d.ts +5 -0
- package/dist/viewer/format.js +28 -0
- package/dist/viewer/index.d.ts +2 -0
- package/dist/viewer/index.js +1 -0
- package/dist/viewer/types.d.ts +172 -0
- package/dist/viewer/types.js +1 -0
- package/dist/viewer/viewer.d.ts +251 -0
- package/dist/viewer/viewer.js +2153 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# @mlightcad/mtext-input-box - Three.js Rich Text Editor
|
|
2
|
+
|
|
3
|
+
Three.js-based MTEXT editor component with built-in IME bridge, cursor/selection rendering, and floating toolbar.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
- MTEXT-style editing in Three.js.
|
|
8
|
+
- Keyboard/mouse/IME editing, selection, formatting, undo/redo.
|
|
9
|
+
- Built-in toolbar (theme/font list/container/offset configurable).
|
|
10
|
+
- Integrates `@mlightcad/mtext-renderer` and `@mlightcad/text-box-cursor`.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @mlightcad/mtext-input-box three @mlightcad/mtext-parser @mlightcad/mtext-renderer
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`@mlightcad/mtext-parser` and `@mlightcad/mtext-renderer` are peer dependencies of `@mlightcad/mtext-input-box`, so your app should install them directly.
|
|
19
|
+
|
|
20
|
+
## Basic Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import * as THREE from 'three';
|
|
24
|
+
import { MTextInputBox } from '@mlightcad/mtext-input-box';
|
|
25
|
+
|
|
26
|
+
const scene = new THREE.Scene();
|
|
27
|
+
const camera = new THREE.OrthographicCamera(0, 1000, 600, 0, -1000, 1000);
|
|
28
|
+
const canvas = document.querySelector<HTMLCanvasElement>('#stage')!;
|
|
29
|
+
|
|
30
|
+
const editor = new MTextInputBox({
|
|
31
|
+
scene,
|
|
32
|
+
camera,
|
|
33
|
+
width: 800,
|
|
34
|
+
position: new THREE.Vector3(80, 520, 0),
|
|
35
|
+
initialText: 'Hello\\PWorld',
|
|
36
|
+
imeTarget: canvas,
|
|
37
|
+
defaultFormat: {
|
|
38
|
+
fontFamily: 'monospace',
|
|
39
|
+
fontSize: 24,
|
|
40
|
+
bold: false,
|
|
41
|
+
italic: false,
|
|
42
|
+
underline: false,
|
|
43
|
+
overline: false,
|
|
44
|
+
strike: false,
|
|
45
|
+
aci: null,
|
|
46
|
+
rgb: 0xffffff
|
|
47
|
+
},
|
|
48
|
+
toolbar: {
|
|
49
|
+
enabled: true,
|
|
50
|
+
theme: 'dark',
|
|
51
|
+
offsetY: 12
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
function animate() {
|
|
56
|
+
requestAnimationFrame(animate);
|
|
57
|
+
editor.update();
|
|
58
|
+
}
|
|
59
|
+
animate();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Notes:
|
|
63
|
+
|
|
64
|
+
- `imeTarget` is required for text input/IME and built-in mouse interactions.
|
|
65
|
+
- For multi-editor scenes, each editor instance can share the same canvas `imeTarget`.
|
|
66
|
+
|
|
67
|
+
## Toolbar
|
|
68
|
+
|
|
69
|
+
The built-in toolbar is a DOM overlay mounted above the active editor and updated automatically when camera/viewport changes.
|
|
70
|
+
|
|
71
|
+
`toolbar` options:
|
|
72
|
+
|
|
73
|
+
- `enabled?: boolean` (default `true`)
|
|
74
|
+
- `theme?: 'light' | 'dark'` (default `dark`)
|
|
75
|
+
- `fontFamilies?: string[]` (default `['Arial', 'Helvetica', 'Verdana', 'Tahoma', 'Trebuchet MS', 'Times New Roman', 'Georgia', 'Courier New', 'system-ui', 'sans-serif', 'serif', 'monospace']`)
|
|
76
|
+
- `container?: HTMLElement` (default `document.body`)
|
|
77
|
+
- `offsetY?: number` (default `10`)
|
|
78
|
+
|
|
79
|
+
Public toolbar-related methods:
|
|
80
|
+
|
|
81
|
+
- `setToolbarTheme('light' | 'dark')`
|
|
82
|
+
- `getToolbarTheme()`
|
|
83
|
+
|
|
84
|
+
## Editor Visibility And Activation
|
|
85
|
+
|
|
86
|
+
- Click inside editor bounds: activate editor and start editing.
|
|
87
|
+
- Click outside active editor bounds: editor closes (`closeEditor()` behavior).
|
|
88
|
+
- Double-click rendered text of a closed editor: re-open (`showEditor()` behavior).
|
|
89
|
+
- Programmatic control:
|
|
90
|
+
- `closeEditor(): void`
|
|
91
|
+
- `showEditor(): boolean`
|
|
92
|
+
|
|
93
|
+
## Important Options
|
|
94
|
+
|
|
95
|
+
- `defaultFormat`: base insert format and renderer fallback style source.
|
|
96
|
+
- `imeTarget`: element used by built-in IME bridge and built-in pointer interactions.
|
|
97
|
+
- `enableWordWrap?: boolean`: enable/disable editor-side wrapping behavior.
|
|
98
|
+
- `showBoundingBox?: boolean`: show editor boundary rectangle.
|
|
99
|
+
- `boundingBoxStyle?: { color?, opacity?, padding?, zOffset? }`: boundary style overrides.
|
|
100
|
+
- `workerUrl?: string | URL`: worker URL for renderer initialization.
|
|
101
|
+
- `cursorStyle` / `selectionStyle`: forwarded to cursor renderer.
|
|
102
|
+
- `toolbar`: built-in toolbar options (`enabled`, `theme`, `fontFamilies`, `container`, `offsetY`).
|
|
103
|
+
|
|
104
|
+
## Input And Interaction
|
|
105
|
+
|
|
106
|
+
Built-in handling:
|
|
107
|
+
|
|
108
|
+
- Keyboard editing (including macOS/Windows shortcut variants)
|
|
109
|
+
- IME composition (via hidden textarea bridge)
|
|
110
|
+
- Mouse selection and word-select:
|
|
111
|
+
- `mousedown`: place cursor / shift-select
|
|
112
|
+
- `mousemove` during drag: update selection
|
|
113
|
+
- `mouseup`: stop drag
|
|
114
|
+
- `dblclick`: select word
|
|
115
|
+
|
|
116
|
+
Advanced control:
|
|
117
|
+
|
|
118
|
+
- `attachIme(target)` / `detachIme()`
|
|
119
|
+
- `attachPointerInteractions(target)` / `detachPointerInteractions()`
|
|
120
|
+
|
|
121
|
+
## Events
|
|
122
|
+
|
|
123
|
+
- `change`
|
|
124
|
+
- `selectionChange`
|
|
125
|
+
- `cursorMove`
|
|
126
|
+
- `show`
|
|
127
|
+
- `close`
|
|
128
|
+
|
|
129
|
+
## Undo / Redo
|
|
130
|
+
|
|
131
|
+
- API:
|
|
132
|
+
- `undo(): boolean`
|
|
133
|
+
- `redo(): boolean`
|
|
134
|
+
- Keyboard:
|
|
135
|
+
- `Ctrl/Cmd + Z`: undo
|
|
136
|
+
- `Ctrl/Cmd + Y`: redo
|
|
137
|
+
- `Ctrl/Cmd + Shift + Z`: redo
|
|
138
|
+
|
|
139
|
+
## Lifecycle
|
|
140
|
+
|
|
141
|
+
- Call `editor.update()` inside your render loop.
|
|
142
|
+
- Call `editor.dispose()` when unmounting to release renderer, cursor, IME, toolbar, and pointer listeners.
|
|
143
|
+
|
|
144
|
+
## Development
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
pnpm --filter @mlightcad/mtext-input-box lint
|
|
148
|
+
pnpm --filter @mlightcad/mtext-input-box build
|
|
149
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MTextDocument } from '../model/document';
|
|
2
|
+
import type { EditorCommand, EditorKeyEventLike } from './types';
|
|
3
|
+
/** Thin adapter for UI keyboard/command integration with selection anchor semantics. */
|
|
4
|
+
export declare class EditorUiAdapter {
|
|
5
|
+
readonly document: MTextDocument;
|
|
6
|
+
private selectionAnchor;
|
|
7
|
+
constructor(document: MTextDocument);
|
|
8
|
+
get anchor(): number | null;
|
|
9
|
+
execute(command: EditorCommand): this;
|
|
10
|
+
/** Maps keyboard-like events into editor commands. Returns true if handled. */
|
|
11
|
+
handleKeyEvent(event: EditorKeyEventLike): boolean;
|
|
12
|
+
private moveCursorTo;
|
|
13
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/** Thin adapter for UI keyboard/command integration with selection anchor semantics. */
|
|
2
|
+
export class EditorUiAdapter {
|
|
3
|
+
document;
|
|
4
|
+
selectionAnchor = null;
|
|
5
|
+
constructor(document) {
|
|
6
|
+
this.document = document;
|
|
7
|
+
}
|
|
8
|
+
get anchor() {
|
|
9
|
+
return this.selectionAnchor;
|
|
10
|
+
}
|
|
11
|
+
execute(command) {
|
|
12
|
+
switch (command.type) {
|
|
13
|
+
case 'setCursor':
|
|
14
|
+
this.document.cursor = command.index;
|
|
15
|
+
this.selectionAnchor = null;
|
|
16
|
+
break;
|
|
17
|
+
case 'setSelection':
|
|
18
|
+
this.document.setSelection(command.start, command.end);
|
|
19
|
+
this.selectionAnchor = command.start;
|
|
20
|
+
break;
|
|
21
|
+
case 'clearSelection':
|
|
22
|
+
this.document.clearSelection();
|
|
23
|
+
this.selectionAnchor = null;
|
|
24
|
+
break;
|
|
25
|
+
case 'moveLeft':
|
|
26
|
+
this.moveCursorTo(this.document.cursor - 1, command.extend ?? false);
|
|
27
|
+
break;
|
|
28
|
+
case 'moveRight':
|
|
29
|
+
this.moveCursorTo(this.document.cursor + 1, command.extend ?? false);
|
|
30
|
+
break;
|
|
31
|
+
case 'moveToStart':
|
|
32
|
+
this.moveCursorTo(0, command.extend ?? false);
|
|
33
|
+
break;
|
|
34
|
+
case 'moveToEnd':
|
|
35
|
+
this.moveCursorTo(this.document.length, command.extend ?? false);
|
|
36
|
+
break;
|
|
37
|
+
case 'selectAll':
|
|
38
|
+
this.document.selectAll();
|
|
39
|
+
this.selectionAnchor = 0;
|
|
40
|
+
break;
|
|
41
|
+
case 'insertText':
|
|
42
|
+
this.document.insertText(command.text, command.style);
|
|
43
|
+
this.selectionAnchor = null;
|
|
44
|
+
break;
|
|
45
|
+
case 'replaceSelection':
|
|
46
|
+
this.document.replaceSelection(command.text, command.style);
|
|
47
|
+
this.selectionAnchor = null;
|
|
48
|
+
break;
|
|
49
|
+
case 'backspace':
|
|
50
|
+
this.document.deleteBackward(command.count ?? 1);
|
|
51
|
+
this.selectionAnchor = null;
|
|
52
|
+
break;
|
|
53
|
+
case 'delete':
|
|
54
|
+
this.document.deleteForward(command.count ?? 1);
|
|
55
|
+
this.selectionAnchor = null;
|
|
56
|
+
break;
|
|
57
|
+
default:
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/** Maps keyboard-like events into editor commands. Returns true if handled. */
|
|
63
|
+
handleKeyEvent(event) {
|
|
64
|
+
const metaOrCtrl = Boolean(event.metaKey || event.ctrlKey);
|
|
65
|
+
if (metaOrCtrl && !event.altKey && event.key.toLowerCase() === 'a') {
|
|
66
|
+
this.execute({ type: 'selectAll' });
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
if (event.key === 'ArrowLeft') {
|
|
70
|
+
this.execute({ type: 'moveLeft', extend: Boolean(event.shiftKey) });
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
if (event.key === 'ArrowRight') {
|
|
74
|
+
this.execute({ type: 'moveRight', extend: Boolean(event.shiftKey) });
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
if (event.key === 'Home') {
|
|
78
|
+
this.execute({ type: 'moveToStart', extend: Boolean(event.shiftKey) });
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
if (event.key === 'End') {
|
|
82
|
+
this.execute({ type: 'moveToEnd', extend: Boolean(event.shiftKey) });
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (event.key === 'Backspace') {
|
|
86
|
+
this.execute({ type: 'backspace' });
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (event.key === 'Delete') {
|
|
90
|
+
this.execute({ type: 'delete' });
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
if (event.key === 'Enter') {
|
|
94
|
+
this.execute({ type: 'insertText', text: '\n' });
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (event.key === 'Tab') {
|
|
98
|
+
this.execute({ type: 'insertText', text: '\t' });
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if (!metaOrCtrl && !event.altKey && event.key.length === 1) {
|
|
102
|
+
this.execute({ type: 'insertText', text: event.key });
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
moveCursorTo(target, extend) {
|
|
108
|
+
const clamped = Math.max(0, Math.min(target, this.document.length));
|
|
109
|
+
if (extend) {
|
|
110
|
+
if (this.selectionAnchor === null) {
|
|
111
|
+
this.selectionAnchor = this.document.selection?.start ?? this.document.cursor;
|
|
112
|
+
}
|
|
113
|
+
this.document.setSelection(this.selectionAnchor, clamped);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
this.document.cursor = clamped;
|
|
117
|
+
this.selectionAnchor = null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { EditorUiAdapter } from './adapter';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { MTextStyle } from '../model/types';
|
|
2
|
+
export type EditorCommand = {
|
|
3
|
+
type: 'setCursor';
|
|
4
|
+
index: number;
|
|
5
|
+
} | {
|
|
6
|
+
type: 'setSelection';
|
|
7
|
+
start: number;
|
|
8
|
+
end: number;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'clearSelection';
|
|
11
|
+
} | {
|
|
12
|
+
type: 'moveLeft';
|
|
13
|
+
extend?: boolean;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'moveRight';
|
|
16
|
+
extend?: boolean;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'moveToStart';
|
|
19
|
+
extend?: boolean;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'moveToEnd';
|
|
22
|
+
extend?: boolean;
|
|
23
|
+
} | {
|
|
24
|
+
type: 'selectAll';
|
|
25
|
+
} | {
|
|
26
|
+
type: 'insertText';
|
|
27
|
+
text: string;
|
|
28
|
+
style?: MTextStyle;
|
|
29
|
+
} | {
|
|
30
|
+
type: 'replaceSelection';
|
|
31
|
+
text: string;
|
|
32
|
+
style?: MTextStyle;
|
|
33
|
+
} | {
|
|
34
|
+
type: 'backspace';
|
|
35
|
+
count?: number;
|
|
36
|
+
} | {
|
|
37
|
+
type: 'delete';
|
|
38
|
+
count?: number;
|
|
39
|
+
};
|
|
40
|
+
export interface EditorKeyEventLike {
|
|
41
|
+
key: string;
|
|
42
|
+
shiftKey?: boolean;
|
|
43
|
+
ctrlKey?: boolean;
|
|
44
|
+
metaKey?: boolean;
|
|
45
|
+
altKey?: boolean;
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
|
|
2
|
+
export { MTextDocument, parseMTextToAst, buildMTextFromAst, insertTextInAst, deleteRangeInAst, replaceRangeInAst } from './model';
|
|
3
|
+
export { EditorUiAdapter } from './controller';
|
|
4
|
+
export { MTextToolbar } from './ui';
|
|
5
|
+
export type { CharFormat, CharScript, CursorDirection, EditorState, LayoutCharBox, CursorLayoutData, MTextBoundingBoxStyle, MTextInputBoxEvent, MTextInputBoxOptions, MTextToolbarOptions, MTextToolbarTheme, MTextEditorEvent, MTextEditorOptions, MTextRendererOutput, SelectionDirection } from './viewer';
|
|
6
|
+
export type { MTextScript, MTextStyle, MTextAst, MTextAstNode, EditorSelection } from './model';
|
|
7
|
+
export type { EditorCommand, EditorKeyEventLike } from './controller';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
|
|
2
|
+
export { MTextDocument, parseMTextToAst, buildMTextFromAst, insertTextInAst, deleteRangeInAst, replaceRangeInAst } from './model';
|
|
3
|
+
export { EditorUiAdapter } from './controller';
|
|
4
|
+
export { MTextToolbar } from './ui';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MTextContext, type MTextParserOptions } from '@mlightcad/mtext-parser';
|
|
2
|
+
import type { MTextAst, MTextStyle } from './types';
|
|
3
|
+
/** Parse mtext into a character-level AST suitable for insert/delete/replace workflows. */
|
|
4
|
+
export declare function parseMTextToAst(content: string, ctx?: MTextContext, options?: Omit<MTextParserOptions, 'yieldPropertyCommands'>): MTextAst;
|
|
5
|
+
/** Build mtext string from AST. */
|
|
6
|
+
export declare function buildMTextFromAst(ast: MTextAst): string;
|
|
7
|
+
/** Insert plain text at a character position. */
|
|
8
|
+
export declare function insertTextInAst(ast: MTextAst, index: number, text: string, style?: MTextStyle): MTextAst;
|
|
9
|
+
/** Delete nodes in [start, start + count). */
|
|
10
|
+
export declare function deleteRangeInAst(ast: MTextAst, start: number, count: number): MTextAst;
|
|
11
|
+
/** Replace nodes in [start, start + count) with plain text. */
|
|
12
|
+
export declare function replaceRangeInAst(ast: MTextAst, start: number, count: number, text: string, style?: MTextStyle): MTextAst;
|
|
13
|
+
export declare function inferInsertStyle(ast: MTextAst, index: number): MTextStyle | undefined;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { MTextContext, MTextParser, TokenType } from '@mlightcad/mtext-parser';
|
|
2
|
+
import { cloneStyle, contextToStyle, hasNodeStyle, serializeStyleDelta } from './style';
|
|
3
|
+
/** Parse mtext into a character-level AST suitable for insert/delete/replace workflows. */
|
|
4
|
+
export function parseMTextToAst(content, ctx, options = {}) {
|
|
5
|
+
const parser = new MTextParser(content, ctx, {
|
|
6
|
+
...options,
|
|
7
|
+
yieldPropertyCommands: false
|
|
8
|
+
});
|
|
9
|
+
const nodes = [];
|
|
10
|
+
for (const token of parser.parse()) {
|
|
11
|
+
const style = contextToStyle(token.ctx);
|
|
12
|
+
switch (token.type) {
|
|
13
|
+
case TokenType.WORD: {
|
|
14
|
+
const value = token.data;
|
|
15
|
+
for (const char of value) {
|
|
16
|
+
nodes.push({ type: 'char', value: char, style: cloneStyle(style) });
|
|
17
|
+
}
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
case TokenType.SPACE:
|
|
21
|
+
nodes.push({ type: 'char', value: ' ', style: cloneStyle(style) });
|
|
22
|
+
break;
|
|
23
|
+
case TokenType.NBSP:
|
|
24
|
+
nodes.push({ type: 'char', value: '\u00a0', style: cloneStyle(style) });
|
|
25
|
+
break;
|
|
26
|
+
case TokenType.TABULATOR:
|
|
27
|
+
nodes.push({ type: 'char', value: '\t', style: cloneStyle(style) });
|
|
28
|
+
break;
|
|
29
|
+
case TokenType.STACK: {
|
|
30
|
+
const [numerator, denominator, divider] = token.data;
|
|
31
|
+
const stackStyle = cloneStyle(style);
|
|
32
|
+
if (divider === '^') {
|
|
33
|
+
if (numerator.length === 0 && denominator.length > 0) {
|
|
34
|
+
stackStyle.script = 'subscript';
|
|
35
|
+
}
|
|
36
|
+
else if (numerator.length > 0 && denominator.length === 0) {
|
|
37
|
+
stackStyle.script = 'superscript';
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
stackStyle.script = 'normal';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
nodes.push({
|
|
44
|
+
type: 'stack',
|
|
45
|
+
numerator,
|
|
46
|
+
denominator,
|
|
47
|
+
divider,
|
|
48
|
+
style: stackStyle
|
|
49
|
+
});
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case TokenType.NEW_PARAGRAPH:
|
|
53
|
+
nodes.push({ type: 'paragraphBreak', style: cloneStyle(style) });
|
|
54
|
+
break;
|
|
55
|
+
case TokenType.NEW_COLUMN:
|
|
56
|
+
nodes.push({ type: 'columnBreak', style: cloneStyle(style) });
|
|
57
|
+
break;
|
|
58
|
+
case TokenType.WRAP_AT_DIMLINE:
|
|
59
|
+
nodes.push({ type: 'wrapAtDimLine', style: cloneStyle(style) });
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { nodes };
|
|
66
|
+
}
|
|
67
|
+
/** Build mtext string from AST. */
|
|
68
|
+
export function buildMTextFromAst(ast) {
|
|
69
|
+
const baseStyle = contextToStyle(new MTextContext());
|
|
70
|
+
let out = '';
|
|
71
|
+
let i = 0;
|
|
72
|
+
while (i < ast.nodes.length) {
|
|
73
|
+
const node = ast.nodes[i];
|
|
74
|
+
if (!node)
|
|
75
|
+
break;
|
|
76
|
+
if (!hasNodeStyle(node)) {
|
|
77
|
+
out += serializeNode(node);
|
|
78
|
+
i += 1;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const runStyle = node.style;
|
|
82
|
+
let runContent = '';
|
|
83
|
+
let j = i;
|
|
84
|
+
while (j < ast.nodes.length) {
|
|
85
|
+
const current = ast.nodes[j];
|
|
86
|
+
if (!current || !hasNodeStyle(current) || !stylesEqual(runStyle, current.style)) {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
runContent += serializeNode(current);
|
|
90
|
+
j += 1;
|
|
91
|
+
}
|
|
92
|
+
if (stylesEqual(runStyle, baseStyle)) {
|
|
93
|
+
out += runContent;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
out += `{${serializeStyleDelta(null, runStyle)}${runContent}}`;
|
|
97
|
+
}
|
|
98
|
+
i = j;
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
/** Insert plain text at a character position. */
|
|
103
|
+
export function insertTextInAst(ast, index, text, style) {
|
|
104
|
+
const safeIndex = clamp(index, 0, ast.nodes.length);
|
|
105
|
+
const insertStyle = style ?? inferInsertStyle(ast, safeIndex) ?? contextToStyle(new MTextContext());
|
|
106
|
+
const newNodes = textToAstNodes(text, insertStyle);
|
|
107
|
+
ast.nodes.splice(safeIndex, 0, ...newNodes);
|
|
108
|
+
return ast;
|
|
109
|
+
}
|
|
110
|
+
/** Delete nodes in [start, start + count). */
|
|
111
|
+
export function deleteRangeInAst(ast, start, count) {
|
|
112
|
+
if (count <= 0)
|
|
113
|
+
return ast;
|
|
114
|
+
const safeStart = clamp(start, 0, ast.nodes.length);
|
|
115
|
+
const safeCount = clamp(count, 0, ast.nodes.length - safeStart);
|
|
116
|
+
ast.nodes.splice(safeStart, safeCount);
|
|
117
|
+
return ast;
|
|
118
|
+
}
|
|
119
|
+
/** Replace nodes in [start, start + count) with plain text. */
|
|
120
|
+
export function replaceRangeInAst(ast, start, count, text, style) {
|
|
121
|
+
const safeStart = clamp(start, 0, ast.nodes.length);
|
|
122
|
+
const safeCount = clamp(count, 0, ast.nodes.length - safeStart);
|
|
123
|
+
const insertStyle = style ?? inferInsertStyle(ast, safeStart) ?? contextToStyle(new MTextContext());
|
|
124
|
+
const newNodes = textToAstNodes(text, insertStyle);
|
|
125
|
+
ast.nodes.splice(safeStart, safeCount, ...newNodes);
|
|
126
|
+
return ast;
|
|
127
|
+
}
|
|
128
|
+
export function inferInsertStyle(ast, index) {
|
|
129
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
130
|
+
const node = ast.nodes[i];
|
|
131
|
+
if (node && hasNodeStyle(node)) {
|
|
132
|
+
return cloneStyle(node.style);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (let i = index; i < ast.nodes.length; i++) {
|
|
136
|
+
const node = ast.nodes[i];
|
|
137
|
+
if (node && hasNodeStyle(node)) {
|
|
138
|
+
return cloneStyle(node.style);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
function encodeChar(char) {
|
|
144
|
+
if (char === '\u00a0')
|
|
145
|
+
return '\\~';
|
|
146
|
+
if (char === '\t')
|
|
147
|
+
return '^I';
|
|
148
|
+
if (char === '\\')
|
|
149
|
+
return '\\\\';
|
|
150
|
+
if (char === '{')
|
|
151
|
+
return '\\{';
|
|
152
|
+
if (char === '}')
|
|
153
|
+
return '\\}';
|
|
154
|
+
if (char === '^')
|
|
155
|
+
return '\\U+005E';
|
|
156
|
+
if (char === '%')
|
|
157
|
+
return '\\U+0025';
|
|
158
|
+
if (char === '\n')
|
|
159
|
+
return '\\P';
|
|
160
|
+
if (char === '\r')
|
|
161
|
+
return '';
|
|
162
|
+
return char;
|
|
163
|
+
}
|
|
164
|
+
function escapeStackPart(value) {
|
|
165
|
+
return value.replace(/[\\;/#^]/g, '\\$&');
|
|
166
|
+
}
|
|
167
|
+
function stylesEqual(a, b) {
|
|
168
|
+
return serializeStyleDelta(a, b) === '';
|
|
169
|
+
}
|
|
170
|
+
function serializeNode(node) {
|
|
171
|
+
switch (node.type) {
|
|
172
|
+
case 'char':
|
|
173
|
+
return encodeChar(node.value);
|
|
174
|
+
case 'stack':
|
|
175
|
+
return serializeStackNode(node.numerator, node.denominator, node.divider);
|
|
176
|
+
case 'paragraphBreak':
|
|
177
|
+
return '\\P';
|
|
178
|
+
case 'columnBreak':
|
|
179
|
+
return '\\N';
|
|
180
|
+
case 'wrapAtDimLine':
|
|
181
|
+
return '\\X';
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function serializeStackNode(numerator, denominator, divider) {
|
|
185
|
+
const top = escapeStackPart(numerator);
|
|
186
|
+
const bottom = escapeStackPart(denominator);
|
|
187
|
+
// mtext-parser expects explicit space for ^-based super/subscript when one side is empty.
|
|
188
|
+
if (divider === '^') {
|
|
189
|
+
if (top.length === 0 && bottom.length > 0) {
|
|
190
|
+
return `\\S^ ${bottom};`;
|
|
191
|
+
}
|
|
192
|
+
if (top.length > 0 && bottom.length === 0) {
|
|
193
|
+
return `\\S${top}^ ;`;
|
|
194
|
+
}
|
|
195
|
+
if (top.length === 0 && bottom.length === 0) {
|
|
196
|
+
return '\\S^ ;';
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return `\\S${top}${divider}${bottom};`;
|
|
200
|
+
}
|
|
201
|
+
function textToAstNodes(text, style) {
|
|
202
|
+
const nodes = [];
|
|
203
|
+
for (const ch of text) {
|
|
204
|
+
if (ch === '\n') {
|
|
205
|
+
nodes.push({ type: 'paragraphBreak', style: cloneStyle(style) });
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
nodes.push({ type: 'char', value: ch, style: cloneStyle(style) });
|
|
209
|
+
}
|
|
210
|
+
return nodes;
|
|
211
|
+
}
|
|
212
|
+
function clamp(value, min, max) {
|
|
213
|
+
return Math.max(min, Math.min(max, value));
|
|
214
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { MTextContext } from '@mlightcad/mtext-parser';
|
|
2
|
+
import type { EditorSelection, MTextAst, MTextStyle } from './types';
|
|
3
|
+
/** Minimal mtext editor document wrapper on top of AST. */
|
|
4
|
+
export declare class MTextDocument {
|
|
5
|
+
private _ast;
|
|
6
|
+
private _cursor;
|
|
7
|
+
private _selection;
|
|
8
|
+
constructor(astOrMText?: MTextAst | string);
|
|
9
|
+
static fromMText(mtext: string, ctx?: MTextContext): MTextDocument;
|
|
10
|
+
get ast(): MTextAst;
|
|
11
|
+
get length(): number;
|
|
12
|
+
get cursor(): number;
|
|
13
|
+
set cursor(value: number);
|
|
14
|
+
get selection(): EditorSelection | null;
|
|
15
|
+
setSelection(start: number, end: number): this;
|
|
16
|
+
clearSelection(): this;
|
|
17
|
+
moveCursor(delta: number, extendSelection?: boolean): this;
|
|
18
|
+
selectAll(): this;
|
|
19
|
+
insertText(text: string, style?: MTextStyle): this;
|
|
20
|
+
deleteBackward(count?: number): this;
|
|
21
|
+
deleteForward(count?: number): this;
|
|
22
|
+
replaceSelection(text: string, style?: MTextStyle): this;
|
|
23
|
+
toMText(): string;
|
|
24
|
+
transaction(work: (doc: MTextDocument) => void): this;
|
|
25
|
+
private hasSelection;
|
|
26
|
+
private getActiveRange;
|
|
27
|
+
private getReferenceStyle;
|
|
28
|
+
private static cloneAst;
|
|
29
|
+
private static normalizeSelection;
|
|
30
|
+
private static clamp;
|
|
31
|
+
}
|