@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.
@@ -0,0 +1,146 @@
1
+ import { MTextContext } from '@mlightcad/mtext-parser';
2
+ import { buildMTextFromAst, deleteRangeInAst, inferInsertStyle, parseMTextToAst, replaceRangeInAst } from './ast';
3
+ import { cloneStyle, contextToStyle } from './style';
4
+ /** Minimal mtext editor document wrapper on top of AST. */
5
+ export class MTextDocument {
6
+ _ast;
7
+ _cursor;
8
+ _selection = null;
9
+ constructor(astOrMText = { nodes: [] }) {
10
+ this._ast =
11
+ typeof astOrMText === 'string' ? parseMTextToAst(astOrMText) : MTextDocument.cloneAst(astOrMText);
12
+ this._cursor = this._ast.nodes.length;
13
+ }
14
+ static fromMText(mtext, ctx) {
15
+ return new MTextDocument(parseMTextToAst(mtext, ctx));
16
+ }
17
+ get ast() {
18
+ return this._ast;
19
+ }
20
+ get length() {
21
+ return this._ast.nodes.length;
22
+ }
23
+ get cursor() {
24
+ return this._cursor;
25
+ }
26
+ set cursor(value) {
27
+ this._cursor = MTextDocument.clamp(value, 0, this.length);
28
+ this._selection = null;
29
+ }
30
+ get selection() {
31
+ return this._selection ? { ...this._selection } : null;
32
+ }
33
+ setSelection(start, end) {
34
+ const normalized = MTextDocument.normalizeSelection(start, end, this.length);
35
+ this._selection = normalized.start === normalized.end ? null : normalized;
36
+ this._cursor = MTextDocument.clamp(end, 0, this.length);
37
+ return this;
38
+ }
39
+ clearSelection() {
40
+ this._selection = null;
41
+ return this;
42
+ }
43
+ moveCursor(delta, extendSelection = false) {
44
+ const next = MTextDocument.clamp(this._cursor + delta, 0, this.length);
45
+ if (extendSelection) {
46
+ const anchor = this._selection ? this._selection.start : this._cursor;
47
+ this.setSelection(anchor, next);
48
+ }
49
+ else {
50
+ this.cursor = next;
51
+ }
52
+ return this;
53
+ }
54
+ selectAll() {
55
+ this.setSelection(0, this.length);
56
+ return this;
57
+ }
58
+ insertText(text, style) {
59
+ const { start, end } = this.getActiveRange();
60
+ replaceRangeInAst(this._ast, start, end - start, text, style);
61
+ this._cursor = start + text.length;
62
+ this._selection = null;
63
+ return this;
64
+ }
65
+ deleteBackward(count = 1) {
66
+ if (this.hasSelection()) {
67
+ const { start, end } = this.getActiveRange();
68
+ deleteRangeInAst(this._ast, start, end - start);
69
+ this._cursor = start;
70
+ this._selection = null;
71
+ return this;
72
+ }
73
+ if (count <= 0 || this._cursor <= 0)
74
+ return this;
75
+ const remove = Math.min(count, this._cursor);
76
+ deleteRangeInAst(this._ast, this._cursor - remove, remove);
77
+ this._cursor -= remove;
78
+ return this;
79
+ }
80
+ deleteForward(count = 1) {
81
+ if (this.hasSelection()) {
82
+ const { start, end } = this.getActiveRange();
83
+ deleteRangeInAst(this._ast, start, end - start);
84
+ this._cursor = start;
85
+ this._selection = null;
86
+ return this;
87
+ }
88
+ if (count <= 0 || this._cursor >= this.length)
89
+ return this;
90
+ deleteRangeInAst(this._ast, this._cursor, count);
91
+ return this;
92
+ }
93
+ replaceSelection(text, style) {
94
+ return this.insertText(text, style);
95
+ }
96
+ toMText() {
97
+ return buildMTextFromAst(this._ast);
98
+ }
99
+ transaction(work) {
100
+ const snapshot = {
101
+ ast: MTextDocument.cloneAst(this._ast),
102
+ cursor: this._cursor,
103
+ selection: this._selection ? { ...this._selection } : null
104
+ };
105
+ try {
106
+ work(this);
107
+ }
108
+ catch (error) {
109
+ this._ast = snapshot.ast;
110
+ this._cursor = snapshot.cursor;
111
+ this._selection = snapshot.selection;
112
+ throw error;
113
+ }
114
+ return this;
115
+ }
116
+ hasSelection() {
117
+ return Boolean(this._selection && this._selection.start !== this._selection.end);
118
+ }
119
+ getActiveRange() {
120
+ if (this._selection)
121
+ return { ...this._selection };
122
+ return { start: this._cursor, end: this._cursor };
123
+ }
124
+ getReferenceStyle(index) {
125
+ return inferInsertStyle(this._ast, index) ?? contextToStyle(new MTextContext());
126
+ }
127
+ static cloneAst(ast) {
128
+ return {
129
+ nodes: ast.nodes.map((node) => {
130
+ if (node.type === 'char')
131
+ return { ...node, style: cloneStyle(node.style) };
132
+ if (node.type === 'stack')
133
+ return { ...node, style: cloneStyle(node.style) };
134
+ return { ...node, style: cloneStyle(node.style) };
135
+ })
136
+ };
137
+ }
138
+ static normalizeSelection(start, end, max) {
139
+ const a = MTextDocument.clamp(start, 0, max);
140
+ const b = MTextDocument.clamp(end, 0, max);
141
+ return a <= b ? { start: a, end: b } : { start: b, end: a };
142
+ }
143
+ static clamp(value, min, max) {
144
+ return Math.max(min, Math.min(max, value));
145
+ }
146
+ }
@@ -0,0 +1,4 @@
1
+ export { parseMTextToAst, buildMTextFromAst, insertTextInAst, deleteRangeInAst, replaceRangeInAst } from './ast';
2
+ export { MTextDocument } from './document';
3
+ export { contextToStyle, cloneStyle, hasNodeStyle, serializeStyleDelta } from './style';
4
+ export type { MTextScript, MTextStyle, MTextAst, MTextAstNode, MTextAstCharNode, MTextAstStackNode, MTextAstParagraphBreakNode, MTextAstColumnBreakNode, MTextAstWrapAtDimLineNode, EditorSelection } from './types';
@@ -0,0 +1,3 @@
1
+ export { parseMTextToAst, buildMTextFromAst, insertTextInAst, deleteRangeInAst, replaceRangeInAst } from './ast';
2
+ export { MTextDocument } from './document';
3
+ export { contextToStyle, cloneStyle, hasNodeStyle, serializeStyleDelta } from './style';
@@ -0,0 +1,6 @@
1
+ import { MTextContext } from '@mlightcad/mtext-parser';
2
+ import type { MTextAstCharNode, MTextAstColumnBreakNode, MTextAstNode, MTextAstParagraphBreakNode, MTextAstStackNode, MTextAstWrapAtDimLineNode, MTextStyle } from './types';
3
+ export declare function contextToStyle(ctx: MTextContext): MTextStyle;
4
+ export declare function cloneStyle(style: MTextStyle): MTextStyle;
5
+ export declare function hasNodeStyle(node: MTextAstNode): node is (MTextAstCharNode | MTextAstStackNode | MTextAstParagraphBreakNode | MTextAstColumnBreakNode | MTextAstWrapAtDimLineNode);
6
+ export declare function serializeStyleDelta(prev: MTextStyle | null, next: MTextStyle): string;
@@ -0,0 +1,186 @@
1
+ import { MTextContext, MTextLineAlignment, MTextParagraphAlignment, rgb2int } from '@mlightcad/mtext-parser';
2
+ function scriptFromAlign(align) {
3
+ if (align === MTextLineAlignment.TOP)
4
+ return 'superscript';
5
+ if (align === MTextLineAlignment.BOTTOM)
6
+ return 'subscript';
7
+ return 'normal';
8
+ }
9
+ function alignFromScript(script) {
10
+ if (script === 'superscript')
11
+ return MTextLineAlignment.TOP;
12
+ if (script === 'subscript')
13
+ return MTextLineAlignment.BOTTOM;
14
+ return MTextLineAlignment.MIDDLE;
15
+ }
16
+ function applyScriptToStyle(style) {
17
+ return {
18
+ ...style,
19
+ align: alignFromScript(style.script)
20
+ };
21
+ }
22
+ export function contextToStyle(ctx) {
23
+ return {
24
+ underline: ctx.underline,
25
+ overline: ctx.overline,
26
+ strikeThrough: ctx.strikeThrough,
27
+ script: scriptFromAlign(ctx.align),
28
+ aci: ctx.aci,
29
+ rgb: ctx.rgb ? [...ctx.rgb] : null,
30
+ align: ctx.align,
31
+ fontFace: { ...ctx.fontFace },
32
+ capHeight: { ...ctx.capHeight },
33
+ widthFactor: { ...ctx.widthFactor },
34
+ charTrackingFactor: { ...ctx.charTrackingFactor },
35
+ oblique: ctx.oblique,
36
+ paragraph: {
37
+ indent: ctx.paragraph.indent,
38
+ left: ctx.paragraph.left,
39
+ right: ctx.paragraph.right,
40
+ align: ctx.paragraph.align,
41
+ tabs: [...ctx.paragraph.tabs]
42
+ }
43
+ };
44
+ }
45
+ export function cloneStyle(style) {
46
+ return {
47
+ underline: style.underline,
48
+ overline: style.overline,
49
+ strikeThrough: style.strikeThrough,
50
+ script: style.script,
51
+ aci: style.aci,
52
+ rgb: style.rgb ? [...style.rgb] : null,
53
+ align: style.align,
54
+ fontFace: { ...style.fontFace },
55
+ capHeight: { ...style.capHeight },
56
+ widthFactor: { ...style.widthFactor },
57
+ charTrackingFactor: { ...style.charTrackingFactor },
58
+ oblique: style.oblique,
59
+ paragraph: {
60
+ indent: style.paragraph.indent,
61
+ left: style.paragraph.left,
62
+ right: style.paragraph.right,
63
+ align: style.paragraph.align,
64
+ tabs: [...style.paragraph.tabs]
65
+ }
66
+ };
67
+ }
68
+ export function hasNodeStyle(node) {
69
+ return (node.type === 'char' ||
70
+ node.type === 'stack' ||
71
+ node.type === 'paragraphBreak' ||
72
+ node.type === 'columnBreak' ||
73
+ node.type === 'wrapAtDimLine');
74
+ }
75
+ function stylesEqual(a, b) {
76
+ return (a.underline === b.underline &&
77
+ a.overline === b.overline &&
78
+ a.strikeThrough === b.strikeThrough &&
79
+ a.script === b.script &&
80
+ a.aci === b.aci &&
81
+ JSON.stringify(a.rgb) === JSON.stringify(b.rgb) &&
82
+ a.align === b.align &&
83
+ a.fontFace.family === b.fontFace.family &&
84
+ a.fontFace.style === b.fontFace.style &&
85
+ a.fontFace.weight === b.fontFace.weight &&
86
+ a.capHeight.value === b.capHeight.value &&
87
+ a.capHeight.isRelative === b.capHeight.isRelative &&
88
+ a.widthFactor.value === b.widthFactor.value &&
89
+ a.widthFactor.isRelative === b.widthFactor.isRelative &&
90
+ a.charTrackingFactor.value === b.charTrackingFactor.value &&
91
+ a.charTrackingFactor.isRelative === b.charTrackingFactor.isRelative &&
92
+ a.oblique === b.oblique &&
93
+ a.paragraph.indent === b.paragraph.indent &&
94
+ a.paragraph.left === b.paragraph.left &&
95
+ a.paragraph.right === b.paragraph.right &&
96
+ a.paragraph.align === b.paragraph.align &&
97
+ JSON.stringify(a.paragraph.tabs) === JSON.stringify(b.paragraph.tabs));
98
+ }
99
+ export function serializeStyleDelta(prev, next) {
100
+ const fromRaw = prev ?? contextToStyle(new MTextContext());
101
+ const from = applyScriptToStyle(fromRaw);
102
+ const nextWithScript = applyScriptToStyle(next);
103
+ if (stylesEqual(from, nextWithScript)) {
104
+ return '';
105
+ }
106
+ let out = '';
107
+ if (from.underline !== nextWithScript.underline)
108
+ out += nextWithScript.underline ? '\\L' : '\\l';
109
+ if (from.overline !== nextWithScript.overline)
110
+ out += nextWithScript.overline ? '\\O' : '\\o';
111
+ if (from.strikeThrough !== nextWithScript.strikeThrough)
112
+ out += nextWithScript.strikeThrough ? '\\K' : '\\k';
113
+ if (from.align !== nextWithScript.align) {
114
+ out += `\\A${nextWithScript.align};`;
115
+ }
116
+ if (from.aci !== nextWithScript.aci || JSON.stringify(from.rgb) !== JSON.stringify(nextWithScript.rgb)) {
117
+ if (nextWithScript.rgb) {
118
+ out += `\\c${rgb2int(nextWithScript.rgb)};`;
119
+ }
120
+ else {
121
+ out += `\\C${nextWithScript.aci ?? 256};`;
122
+ }
123
+ }
124
+ if (from.fontFace.family !== nextWithScript.fontFace.family ||
125
+ from.fontFace.style !== nextWithScript.fontFace.style ||
126
+ from.fontFace.weight !== nextWithScript.fontFace.weight) {
127
+ const bold = nextWithScript.fontFace.weight >= 700 ? 1 : 0;
128
+ const italic = nextWithScript.fontFace.style === 'Italic' ? 1 : 0;
129
+ out += `\\f${nextWithScript.fontFace.family}|b${bold}|i${italic};`;
130
+ }
131
+ if (from.capHeight.value !== nextWithScript.capHeight.value ||
132
+ from.capHeight.isRelative !== nextWithScript.capHeight.isRelative) {
133
+ out += `\\H${formatFactor(nextWithScript.capHeight)};`;
134
+ }
135
+ if (from.widthFactor.value !== nextWithScript.widthFactor.value ||
136
+ from.widthFactor.isRelative !== nextWithScript.widthFactor.isRelative) {
137
+ out += `\\W${formatFactor(nextWithScript.widthFactor)};`;
138
+ }
139
+ if (from.charTrackingFactor.value !== nextWithScript.charTrackingFactor.value ||
140
+ from.charTrackingFactor.isRelative !== nextWithScript.charTrackingFactor.isRelative) {
141
+ out += `\\T${formatFactor(nextWithScript.charTrackingFactor)};`;
142
+ }
143
+ if (from.oblique !== nextWithScript.oblique) {
144
+ out += `\\Q${formatNumber(nextWithScript.oblique)};`;
145
+ }
146
+ if (JSON.stringify(from.paragraph) !== JSON.stringify(nextWithScript.paragraph)) {
147
+ out += `\\p${serializeParagraph(nextWithScript.paragraph)};`;
148
+ }
149
+ return out;
150
+ }
151
+ function formatFactor(value) {
152
+ return `${formatNumber(value.value)}${value.isRelative ? 'x' : ''}`;
153
+ }
154
+ function formatNumber(value) {
155
+ return Number.isInteger(value) ? value.toString() : value.toString();
156
+ }
157
+ function serializeParagraph(paragraph) {
158
+ const parts = [
159
+ `i${formatNumber(paragraph.indent)}`,
160
+ `l${formatNumber(paragraph.left)}`,
161
+ `r${formatNumber(paragraph.right)}`
162
+ ];
163
+ if (paragraph.align !== MTextParagraphAlignment.DEFAULT) {
164
+ parts.push(`q${paragraphAlignToChar(paragraph.align)}`);
165
+ }
166
+ if (paragraph.tabs.length > 0) {
167
+ parts.push(`t${paragraph.tabs.map((tab) => String(tab)).join(',')}`);
168
+ }
169
+ return parts.join(',');
170
+ }
171
+ function paragraphAlignToChar(align) {
172
+ switch (align) {
173
+ case MTextParagraphAlignment.LEFT:
174
+ return 'l';
175
+ case MTextParagraphAlignment.RIGHT:
176
+ return 'r';
177
+ case MTextParagraphAlignment.CENTER:
178
+ return 'c';
179
+ case MTextParagraphAlignment.JUSTIFIED:
180
+ return 'j';
181
+ case MTextParagraphAlignment.DISTRIBUTED:
182
+ return 'd';
183
+ default:
184
+ return 'x';
185
+ }
186
+ }
@@ -0,0 +1,57 @@
1
+ import type { FactorValue, FontFace, MTextLineAlignment, ParagraphProperties, RGB } from '@mlightcad/mtext-parser';
2
+ export type MTextScript = 'normal' | 'superscript' | 'subscript';
3
+ /** Snapshot of resolved text style for editable AST nodes. */
4
+ export interface MTextStyle {
5
+ underline: boolean;
6
+ overline: boolean;
7
+ strikeThrough: boolean;
8
+ script: MTextScript;
9
+ aci: number | null;
10
+ rgb: RGB | null;
11
+ align: MTextLineAlignment;
12
+ fontFace: FontFace;
13
+ capHeight: FactorValue;
14
+ widthFactor: FactorValue;
15
+ charTrackingFactor: FactorValue;
16
+ oblique: number;
17
+ paragraph: ParagraphProperties;
18
+ }
19
+ /** Character node for rich text editing. */
20
+ export interface MTextAstCharNode {
21
+ type: 'char';
22
+ value: string;
23
+ style: MTextStyle;
24
+ }
25
+ /** Fraction/stacking node (e.g. \S1/2;). */
26
+ export interface MTextAstStackNode {
27
+ type: 'stack';
28
+ numerator: string;
29
+ denominator: string;
30
+ divider: string;
31
+ style: MTextStyle;
32
+ }
33
+ /** Paragraph break node (maps to \P). */
34
+ export interface MTextAstParagraphBreakNode {
35
+ type: 'paragraphBreak';
36
+ style: MTextStyle;
37
+ }
38
+ /** Column break node (maps to \N). */
39
+ export interface MTextAstColumnBreakNode {
40
+ type: 'columnBreak';
41
+ style: MTextStyle;
42
+ }
43
+ /** Wrap-at-dimension-line node (maps to \X). */
44
+ export interface MTextAstWrapAtDimLineNode {
45
+ type: 'wrapAtDimLine';
46
+ style: MTextStyle;
47
+ }
48
+ export type MTextAstNode = MTextAstCharNode | MTextAstStackNode | MTextAstParagraphBreakNode | MTextAstColumnBreakNode | MTextAstWrapAtDimLineNode;
49
+ /** Character-level editable AST model. */
50
+ export interface MTextAst {
51
+ nodes: MTextAstNode[];
52
+ }
53
+ /** Selection range in AST node indexes. */
54
+ export interface EditorSelection {
55
+ start: number;
56
+ end: number;
57
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ export declare const toolbarIcons: {
2
+ readonly bold: "<svg viewBox=\"0 0 24 24\"><path d=\"M7.8 19c-.3 0-.5 0-.6-.2l-.2-.5V5.7c0-.2 0-.4.2-.5l.6-.2h5c1.5 0 2.7.3 3.5 1 .7.6 1.1 1.4 1.1 2.5a3 3 0 0 1-.6 1.9c-.4.6-1 1-1.6 1.2.4.1.9.3 1.3.6s.8.7 1 1.2c.4.4.5 1 .5 1.6 0 1.3-.4 2.3-1.3 3-.8.7-2.1 1-3.8 1H7.8Zm5-8.3c.6 0 1.2-.1 1.6-.5.4-.3.6-.7.6-1.3 0-1.1-.8-1.7-2.3-1.7H9.3v3.5h3.4Zm.5 6c.7 0 1.3-.1 1.7-.4.4-.4.6-.9.6-1.5s-.2-1-.7-1.4c-.4-.3-1-.4-2-.4H9.4v3.8h4Z\" fill-rule=\"evenodd\"></path></svg>";
3
+ readonly italic: "<svg viewBox=\"0 0 24 24\"><path d=\"m16.7 4.7-.1.9h-.3c-.6 0-1 0-1.4.3-.3.3-.4.6-.5 1.1l-2.1 9.8v.6c0 .5.4.8 1.4.8h.2l-.2.8H8l.2-.8h.2c1.1 0 1.8-.5 2-1.5l2-9.8.1-.5c0-.6-.4-.8-1.4-.8h-.3l.2-.9h5.8Z\" fill-rule=\"evenodd\"></path></svg>";
4
+ readonly underline: "<svg viewBox=\"0 0 24 24\"><path d=\"M16 5c.6 0 1 .4 1 1v5.5a4 4 0 0 1-.4 1.8l-1 1.4a5.3 5.3 0 0 1-5.5 1 5 5 0 0 1-1.6-1c-.5-.4-.8-.9-1.1-1.4a4 4 0 0 1-.4-1.8V6c0-.6.4-1 1-1s1 .4 1 1v5.5c0 .3 0 .6.2 1l.6.7a3.3 3.3 0 0 0 2.2.8 3.4 3.4 0 0 0 2.2-.8c.3-.2.4-.5.6-.8l.2-.9V6c0-.6.4-1 1-1ZM8 17h8c.6 0 1 .4 1 1s-.4 1-1 1H8a1 1 0 0 1 0-2Z\" fill-rule=\"evenodd\"></path></svg>";
5
+ readonly overline: "<svg viewBox=\"0 0 24 24\"><path d=\"M5 4h14v1.5H5V4zm7 3.5c3.04 0 5.5 2.46 5.5 5.5v4.5h-2.25v-4.5c0-1.79-1.46-3.25-3.25-3.25S8.75 11.21 8.75 13v4.5H6.5V13c0-3.04 2.46-5.5 5.5-5.5z\"></path></svg>";
6
+ readonly strike: "<svg viewBox=\"0 0 24 24\"><g fill-rule=\"evenodd\"><path d=\"M15.6 8.5c-.5-.7-1-1.1-1.3-1.3-.6-.4-1.3-.6-2-.6-2.7 0-2.8 1.7-2.8 2.1 0 1.6 1.8 2 3.2 2.3 4.4.9 4.6 2.8 4.6 3.9 0 1.4-.7 4.1-5 4.1A6.2 6.2 0 0 1 7 16.4l1.5-1.1c.4.6 1.6 2 3.7 2 1.6 0 2.5-.4 3-1.2.4-.8.3-2-.8-2.6-.7-.4-1.6-.7-2.9-1-1-.2-3.9-.8-3.9-3.6C7.6 6 10.3 5 12.4 5c2.9 0 4.2 1.6 4.7 2.4l-1.5 1.1Z\"></path><path d=\"M5 11h14a1 1 0 0 1 0 2H5a1 1 0 0 1 0-2Z\" fill-rule=\"nonzero\"></path></g></svg>";
7
+ readonly superscript: "<svg viewBox=\"0 0 24 24\"><path d=\"M15 9.4 10.4 14l4.6 4.6-1.4 1.4L9 15.4 4.4 20 3 18.6 7.6 14 3 9.4 4.4 8 9 12.6 13.6 8 15 9.4Zm5.9 1.6h-5v-1l1-.8 1.7-1.6c.3-.5.5-.9.5-1.3 0-.3 0-.5-.2-.7-.2-.2-.5-.3-.9-.3l-.8.2-.7.4-.4-1.2c.2-.2.5-.4 1-.5.3-.2.8-.2 1.2-.2.8 0 1.4.2 1.8.6.4.4.6 1 .6 1.6 0 .5-.2 1-.5 1.5l-1.3 1.4-.6.5h2.6V11Z\" fill-rule=\"nonzero\"></path></svg>";
8
+ readonly subscript: "<svg viewBox=\"0 0 24 24\"><path d=\"m10.4 10 4.6 4.6-1.4 1.4L9 11.4 4.4 16 3 14.6 7.6 10 3 5.4 4.4 4 9 8.6 13.6 4 15 5.4 10.4 10ZM21 19h-5v-1l1-.8 1.7-1.6c.3-.4.5-.8.5-1.2 0-.3 0-.6-.2-.7-.2-.2-.5-.3-.9-.3a2 2 0 0 0-.8.2l-.7.3-.4-1.1 1-.6 1.2-.2c.8 0 1.4.3 1.8.7.4.4.6.9.6 1.5s-.2 1.1-.5 1.6a8 8 0 0 1-1.3 1.3l-.6.6h2.6V19Z\" fill-rule=\"nonzero\"></path></svg>";
9
+ readonly stack: "<svg viewBox=\"0 0 24 24\"><path d=\"M12.4065 5.44832C12.8151 5.44832 13.1994 5.56542 13.5595 5.7996C13.9265 6.0267 14.2208 6.36024 14.4424 6.80022C14.664 7.23312 14.7748 7.74407 14.7748 8.33309C14.7748 8.9292 14.664 9.44725 14.4424 9.88724C14.2208 10.3201 13.9265 10.6501 13.5595 10.8772C13.1994 11.1043 12.8151 11.2179 12.4065 11.2179C12.0464 11.2179 11.7037 11.1291 11.3782 10.9517C11.0527 10.7743 10.7931 10.5082 10.5992 10.1534C10.5715 10.4585 10.5507 10.7743 10.5368 11.1008H9.75781V3H10.6407V6.4383C10.8415 6.11185 11.0977 5.86702 11.4094 5.7038C11.721 5.53348 12.0534 5.44832 12.4065 5.44832ZM12.2923 10.3663C12.7631 10.3663 13.1405 10.1888 13.4245 9.83402C13.7084 9.47919 13.8503 8.97888 13.8503 8.33309C13.8503 7.69439 13.7084 7.19763 13.4245 6.8428C13.1405 6.48797 12.7631 6.31056 12.2923 6.31056C11.8422 6.31056 11.4544 6.47378 11.1289 6.80022C10.8034 7.12667 10.6407 7.63762 10.6407 8.33309C10.6407 8.80856 10.7203 9.19887 10.8796 9.50402C11.0389 9.80918 11.2432 10.0292 11.4925 10.164C11.7417 10.2988 12.0083 10.3663 12.2923 10.3663Z\"></path><path d=\"M12.0949 14.9531C12.7043 14.9531 13.2029 15.1447 13.5906 15.5279C13.9784 15.9041 14.1723 16.4434 14.1723 17.146V20.6056H13.3933C13.3725 20.2436 13.3587 20.0094 13.3517 19.903C13.1856 20.1727 12.9605 20.3785 12.6766 20.5204C12.3996 20.6552 12.0811 20.7227 11.721 20.7227C11.3747 20.7227 11.0493 20.6588 10.7446 20.531C10.4468 20.3962 10.2079 20.2011 10.0279 19.9456C9.84783 19.683 9.75781 19.3708 9.75781 19.0088C9.75781 18.4624 9.97248 18.033 10.4018 17.7208C10.8381 17.4014 11.399 17.2418 12.0845 17.2418C12.4723 17.2418 12.8739 17.295 13.2894 17.4014V17.3056C13.2894 16.7876 13.1856 16.4044 12.9778 16.156C12.777 15.9005 12.4688 15.7728 12.0534 15.7728C11.721 15.7728 11.444 15.8437 11.2224 15.9857C11.0008 16.1276 10.8138 16.337 10.6615 16.6137L9.98633 16.2199C10.1525 15.8367 10.4226 15.5315 10.7965 15.3044C11.1774 15.0702 11.6102 14.9531 12.0949 14.9531ZM13.2894 18.1572C12.8532 18.0437 12.455 17.9869 12.0949 17.9869C11.6448 17.9869 11.2951 18.0756 11.0458 18.253C10.8034 18.4305 10.6823 18.6788 10.6823 18.9982C10.6823 19.2962 10.7861 19.5304 10.9939 19.7007C11.2016 19.864 11.4717 19.9456 11.8041 19.9456C12.2126 19.9456 12.5623 19.8249 12.8532 19.5836C13.144 19.3353 13.2894 18.9946 13.2894 18.5617V18.1572Z\"\"></path><rect x=\"6.02344\" y=\"12.7119\" width=\"11.9532\" height=\"0.747077\"></rect></svg>";
10
+ };
11
+ export type ToolbarIconName = keyof typeof toolbarIcons;
@@ -0,0 +1,10 @@
1
+ export const toolbarIcons = {
2
+ bold: '<svg viewBox="0 0 24 24"><path d="M7.8 19c-.3 0-.5 0-.6-.2l-.2-.5V5.7c0-.2 0-.4.2-.5l.6-.2h5c1.5 0 2.7.3 3.5 1 .7.6 1.1 1.4 1.1 2.5a3 3 0 0 1-.6 1.9c-.4.6-1 1-1.6 1.2.4.1.9.3 1.3.6s.8.7 1 1.2c.4.4.5 1 .5 1.6 0 1.3-.4 2.3-1.3 3-.8.7-2.1 1-3.8 1H7.8Zm5-8.3c.6 0 1.2-.1 1.6-.5.4-.3.6-.7.6-1.3 0-1.1-.8-1.7-2.3-1.7H9.3v3.5h3.4Zm.5 6c.7 0 1.3-.1 1.7-.4.4-.4.6-.9.6-1.5s-.2-1-.7-1.4c-.4-.3-1-.4-2-.4H9.4v3.8h4Z" fill-rule="evenodd"></path></svg>',
3
+ italic: '<svg viewBox="0 0 24 24"><path d="m16.7 4.7-.1.9h-.3c-.6 0-1 0-1.4.3-.3.3-.4.6-.5 1.1l-2.1 9.8v.6c0 .5.4.8 1.4.8h.2l-.2.8H8l.2-.8h.2c1.1 0 1.8-.5 2-1.5l2-9.8.1-.5c0-.6-.4-.8-1.4-.8h-.3l.2-.9h5.8Z" fill-rule="evenodd"></path></svg>',
4
+ underline: '<svg viewBox="0 0 24 24"><path d="M16 5c.6 0 1 .4 1 1v5.5a4 4 0 0 1-.4 1.8l-1 1.4a5.3 5.3 0 0 1-5.5 1 5 5 0 0 1-1.6-1c-.5-.4-.8-.9-1.1-1.4a4 4 0 0 1-.4-1.8V6c0-.6.4-1 1-1s1 .4 1 1v5.5c0 .3 0 .6.2 1l.6.7a3.3 3.3 0 0 0 2.2.8 3.4 3.4 0 0 0 2.2-.8c.3-.2.4-.5.6-.8l.2-.9V6c0-.6.4-1 1-1ZM8 17h8c.6 0 1 .4 1 1s-.4 1-1 1H8a1 1 0 0 1 0-2Z" fill-rule="evenodd"></path></svg>',
5
+ overline: '<svg viewBox="0 0 24 24"><path d="M5 4h14v1.5H5V4zm7 3.5c3.04 0 5.5 2.46 5.5 5.5v4.5h-2.25v-4.5c0-1.79-1.46-3.25-3.25-3.25S8.75 11.21 8.75 13v4.5H6.5V13c0-3.04 2.46-5.5 5.5-5.5z"></path></svg>',
6
+ strike: '<svg viewBox="0 0 24 24"><g fill-rule="evenodd"><path d="M15.6 8.5c-.5-.7-1-1.1-1.3-1.3-.6-.4-1.3-.6-2-.6-2.7 0-2.8 1.7-2.8 2.1 0 1.6 1.8 2 3.2 2.3 4.4.9 4.6 2.8 4.6 3.9 0 1.4-.7 4.1-5 4.1A6.2 6.2 0 0 1 7 16.4l1.5-1.1c.4.6 1.6 2 3.7 2 1.6 0 2.5-.4 3-1.2.4-.8.3-2-.8-2.6-.7-.4-1.6-.7-2.9-1-1-.2-3.9-.8-3.9-3.6C7.6 6 10.3 5 12.4 5c2.9 0 4.2 1.6 4.7 2.4l-1.5 1.1Z"></path><path d="M5 11h14a1 1 0 0 1 0 2H5a1 1 0 0 1 0-2Z" fill-rule="nonzero"></path></g></svg>',
7
+ superscript: '<svg viewBox="0 0 24 24"><path d="M15 9.4 10.4 14l4.6 4.6-1.4 1.4L9 15.4 4.4 20 3 18.6 7.6 14 3 9.4 4.4 8 9 12.6 13.6 8 15 9.4Zm5.9 1.6h-5v-1l1-.8 1.7-1.6c.3-.5.5-.9.5-1.3 0-.3 0-.5-.2-.7-.2-.2-.5-.3-.9-.3l-.8.2-.7.4-.4-1.2c.2-.2.5-.4 1-.5.3-.2.8-.2 1.2-.2.8 0 1.4.2 1.8.6.4.4.6 1 .6 1.6 0 .5-.2 1-.5 1.5l-1.3 1.4-.6.5h2.6V11Z" fill-rule="nonzero"></path></svg>',
8
+ subscript: '<svg viewBox="0 0 24 24"><path d="m10.4 10 4.6 4.6-1.4 1.4L9 11.4 4.4 16 3 14.6 7.6 10 3 5.4 4.4 4 9 8.6 13.6 4 15 5.4 10.4 10ZM21 19h-5v-1l1-.8 1.7-1.6c.3-.4.5-.8.5-1.2 0-.3 0-.6-.2-.7-.2-.2-.5-.3-.9-.3a2 2 0 0 0-.8.2l-.7.3-.4-1.1 1-.6 1.2-.2c.8 0 1.4.3 1.8.7.4.4.6.9.6 1.5s-.2 1.1-.5 1.6a8 8 0 0 1-1.3 1.3l-.6.6h2.6V19Z" fill-rule="nonzero"></path></svg>',
9
+ stack: '<svg viewBox="0 0 24 24"><path d="M12.4065 5.44832C12.8151 5.44832 13.1994 5.56542 13.5595 5.7996C13.9265 6.0267 14.2208 6.36024 14.4424 6.80022C14.664 7.23312 14.7748 7.74407 14.7748 8.33309C14.7748 8.9292 14.664 9.44725 14.4424 9.88724C14.2208 10.3201 13.9265 10.6501 13.5595 10.8772C13.1994 11.1043 12.8151 11.2179 12.4065 11.2179C12.0464 11.2179 11.7037 11.1291 11.3782 10.9517C11.0527 10.7743 10.7931 10.5082 10.5992 10.1534C10.5715 10.4585 10.5507 10.7743 10.5368 11.1008H9.75781V3H10.6407V6.4383C10.8415 6.11185 11.0977 5.86702 11.4094 5.7038C11.721 5.53348 12.0534 5.44832 12.4065 5.44832ZM12.2923 10.3663C12.7631 10.3663 13.1405 10.1888 13.4245 9.83402C13.7084 9.47919 13.8503 8.97888 13.8503 8.33309C13.8503 7.69439 13.7084 7.19763 13.4245 6.8428C13.1405 6.48797 12.7631 6.31056 12.2923 6.31056C11.8422 6.31056 11.4544 6.47378 11.1289 6.80022C10.8034 7.12667 10.6407 7.63762 10.6407 8.33309C10.6407 8.80856 10.7203 9.19887 10.8796 9.50402C11.0389 9.80918 11.2432 10.0292 11.4925 10.164C11.7417 10.2988 12.0083 10.3663 12.2923 10.3663Z"></path><path d="M12.0949 14.9531C12.7043 14.9531 13.2029 15.1447 13.5906 15.5279C13.9784 15.9041 14.1723 16.4434 14.1723 17.146V20.6056H13.3933C13.3725 20.2436 13.3587 20.0094 13.3517 19.903C13.1856 20.1727 12.9605 20.3785 12.6766 20.5204C12.3996 20.6552 12.0811 20.7227 11.721 20.7227C11.3747 20.7227 11.0493 20.6588 10.7446 20.531C10.4468 20.3962 10.2079 20.2011 10.0279 19.9456C9.84783 19.683 9.75781 19.3708 9.75781 19.0088C9.75781 18.4624 9.97248 18.033 10.4018 17.7208C10.8381 17.4014 11.399 17.2418 12.0845 17.2418C12.4723 17.2418 12.8739 17.295 13.2894 17.4014V17.3056C13.2894 16.7876 13.1856 16.4044 12.9778 16.156C12.777 15.9005 12.4688 15.7728 12.0534 15.7728C11.721 15.7728 11.444 15.8437 11.2224 15.9857C11.0008 16.1276 10.8138 16.337 10.6615 16.6137L9.98633 16.2199C10.1525 15.8367 10.4226 15.5315 10.7965 15.3044C11.1774 15.0702 11.6102 14.9531 12.0949 14.9531ZM13.2894 18.1572C12.8532 18.0437 12.455 17.9869 12.0949 17.9869C11.6448 17.9869 11.2951 18.0756 11.0458 18.253C10.8034 18.4305 10.6823 18.6788 10.6823 18.9982C10.6823 19.2962 10.7861 19.5304 10.9939 19.7007C11.2016 19.864 11.4717 19.9456 11.8041 19.9456C12.2126 19.9456 12.5623 19.8249 12.8532 19.5836C13.144 19.3353 13.2894 18.9946 13.2894 18.5617V18.1572Z""></path><rect x="6.02344" y="12.7119" width="11.9532" height="0.747077"></rect></svg>',
10
+ };
@@ -0,0 +1,2 @@
1
+ export { MTextToolbar } from './toolbar';
2
+ export { toolbarIcons, type ToolbarIconName } from './icons';
@@ -0,0 +1,2 @@
1
+ export { MTextToolbar } from './toolbar';
2
+ export { toolbarIcons } from './icons';
@@ -0,0 +1,34 @@
1
+ import type { CharFormat, MTextToolbarTheme } from '../viewer/types';
2
+ export interface ToolbarOptions {
3
+ anchorElement: HTMLElement;
4
+ container?: HTMLElement;
5
+ theme: MTextToolbarTheme;
6
+ fontFamilies?: string[];
7
+ onFormatChange: (partial: Partial<CharFormat>) => void;
8
+ onToggleStack: () => void;
9
+ onToggleSuperscript?: () => boolean;
10
+ onToggleSubscript?: () => boolean;
11
+ }
12
+ export type ToolbarSessionOptions = Omit<ToolbarOptions, 'container' | 'theme' | 'fontFamilies'>;
13
+ export declare class MTextToolbar {
14
+ private anchorElement;
15
+ private readonly root;
16
+ private readonly controls;
17
+ private onFormatChange;
18
+ private onToggleStack;
19
+ private onToggleSuperscript;
20
+ private onToggleSubscript;
21
+ private theme;
22
+ constructor(options: ToolbarOptions);
23
+ setTheme(theme: MTextToolbarTheme): void;
24
+ getTheme(): MTextToolbarTheme;
25
+ setVisible(visible: boolean): void;
26
+ setSession(options: ToolbarSessionOptions): void;
27
+ setAnchor(clientX: number, clientY: number): void;
28
+ setFormat(format: CharFormat): void;
29
+ dispose(): void;
30
+ setStackActive(active: boolean): void;
31
+ private setToggleState;
32
+ private ensureSelectValue;
33
+ private bindEvents;
34
+ }