@mlightcad/mtext-input-box 0.1.2 → 0.1.3
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 +19 -2
- package/dist/ui/toolbar.js +30 -9
- package/dist/viewer/types.d.ts +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -92,14 +92,31 @@ Custom color picker factory context:
|
|
|
92
92
|
- `initialColor: '#RRGGBB'`: initial color.
|
|
93
93
|
- `onChange(hexColor)`: call this when user picks a new color.
|
|
94
94
|
|
|
95
|
+
Custom color picker instance methods:
|
|
96
|
+
|
|
97
|
+
- `setValue(color: MTextColor)`: sync when editor format changes (color may be ACI or RGB).
|
|
98
|
+
- `setTheme(theme)`: optional hook for theme changes.
|
|
99
|
+
- `dispose()`: cleanup any mounted resources.
|
|
100
|
+
|
|
95
101
|
Example (mount a Vue color picker):
|
|
96
102
|
|
|
97
103
|
```ts
|
|
98
104
|
import { createApp, h, ref } from 'vue';
|
|
105
|
+
import { getColorByIndex } from '@mlightcad/mtext-renderer';
|
|
106
|
+
import { MTextColor } from '@mlightcad/mtext-parser';
|
|
99
107
|
|
|
100
108
|
toolbar: {
|
|
101
109
|
colorPicker: ({ container, initialColor, theme, onChange }) => {
|
|
102
110
|
const color = ref(initialColor);
|
|
111
|
+
const toHex = (value: MTextColor) => {
|
|
112
|
+
if (value.isRgb && value.rgbValue !== null) {
|
|
113
|
+
return `#${value.rgbValue.toString(16).padStart(6, '0')}`;
|
|
114
|
+
}
|
|
115
|
+
if (value.isAci && value.aci !== null) {
|
|
116
|
+
return `#${getColorByIndex(value.aci).toString(16).padStart(6, '0')}`;
|
|
117
|
+
}
|
|
118
|
+
return '#ffffff';
|
|
119
|
+
};
|
|
103
120
|
const app = createApp({
|
|
104
121
|
render() {
|
|
105
122
|
return h(MyVueColorPicker, {
|
|
@@ -116,8 +133,8 @@ toolbar: {
|
|
|
116
133
|
app.mount(container);
|
|
117
134
|
|
|
118
135
|
return {
|
|
119
|
-
setValue(next:
|
|
120
|
-
color.value = next;
|
|
136
|
+
setValue(next: MTextColor) {
|
|
137
|
+
color.value = toHex(next);
|
|
121
138
|
},
|
|
122
139
|
setTheme(nextTheme: 'light' | 'dark') {
|
|
123
140
|
// Optional: forward theme into your picker state if needed.
|
package/dist/ui/toolbar.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getColorByIndex } from '@mlightcad/mtext-renderer';
|
|
2
|
+
import { MTextColor } from '@mlightcad/mtext-parser';
|
|
1
3
|
import { toolbarIcons } from './icons';
|
|
2
4
|
const STYLE_ID = 'mlightcad-mtext-toolbar-style';
|
|
3
5
|
const DEFAULT_FONT_FAMILIES = [
|
|
@@ -147,6 +149,28 @@ function hexToColorNumber(hex) {
|
|
|
147
149
|
return 0xffffff;
|
|
148
150
|
return Number.parseInt(hex.slice(1), 16);
|
|
149
151
|
}
|
|
152
|
+
function mtextColorToHex(color) {
|
|
153
|
+
if (color.isRgb && color.rgbValue !== null) {
|
|
154
|
+
return colorNumberToHex(color.rgbValue);
|
|
155
|
+
}
|
|
156
|
+
if (color.isAci && color.aci !== null) {
|
|
157
|
+
return colorNumberToHex(getColorByIndex(color.aci));
|
|
158
|
+
}
|
|
159
|
+
return '#ffffff';
|
|
160
|
+
}
|
|
161
|
+
function formatToMTextColor(format) {
|
|
162
|
+
const color = new MTextColor();
|
|
163
|
+
if (format.aci !== null && Number.isFinite(format.aci)) {
|
|
164
|
+
color.aci = format.aci;
|
|
165
|
+
return color;
|
|
166
|
+
}
|
|
167
|
+
if (format.rgb !== null) {
|
|
168
|
+
color.rgbValue = normalizeColorNumber(format.rgb);
|
|
169
|
+
return color;
|
|
170
|
+
}
|
|
171
|
+
color.rgbValue = 0xffffff;
|
|
172
|
+
return color;
|
|
173
|
+
}
|
|
150
174
|
function normalizeHexColor(hex) {
|
|
151
175
|
const normalized = hex.trim().toLowerCase();
|
|
152
176
|
if (/^#[0-9a-f]{6}$/.test(normalized))
|
|
@@ -279,8 +303,8 @@ export class MTextToolbar {
|
|
|
279
303
|
setFormat(format) {
|
|
280
304
|
this.ensureSelectValue(format.fontFamily);
|
|
281
305
|
this.controls.fontSize.value = String(Math.max(1, Math.round(format.fontSize)));
|
|
282
|
-
const color = format
|
|
283
|
-
this.colorPicker.setValue(
|
|
306
|
+
const color = formatToMTextColor(format);
|
|
307
|
+
this.colorPicker.setValue(color);
|
|
284
308
|
this.setToggleState(this.controls.boldBtn, format.bold);
|
|
285
309
|
this.setToggleState(this.controls.italicBtn, format.italic);
|
|
286
310
|
this.setToggleState(this.controls.underlineBtn, format.underline);
|
|
@@ -381,8 +405,8 @@ export class MTextToolbar {
|
|
|
381
405
|
});
|
|
382
406
|
return {
|
|
383
407
|
host: input,
|
|
384
|
-
setValue: (
|
|
385
|
-
const normalized = normalizeHexColor(
|
|
408
|
+
setValue: (color) => {
|
|
409
|
+
const normalized = normalizeHexColor(mtextColorToHex(color));
|
|
386
410
|
if (normalized)
|
|
387
411
|
input.value = normalized;
|
|
388
412
|
},
|
|
@@ -404,11 +428,8 @@ export class MTextToolbar {
|
|
|
404
428
|
const typed = (instance ?? {});
|
|
405
429
|
return {
|
|
406
430
|
host,
|
|
407
|
-
setValue: (
|
|
408
|
-
|
|
409
|
-
if (!normalized)
|
|
410
|
-
return;
|
|
411
|
-
typed.setValue?.(normalized);
|
|
431
|
+
setValue: (color) => {
|
|
432
|
+
typed.setValue?.(color);
|
|
412
433
|
},
|
|
413
434
|
setTheme: (theme) => {
|
|
414
435
|
typed.setTheme?.(theme);
|
package/dist/viewer/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Box, CursorStyle, SelectionStyle } from '@mlightcad/text-box-cursor';
|
|
2
|
+
import type { MTextColor } from '@mlightcad/mtext-parser';
|
|
2
3
|
import type * as THREE from 'three';
|
|
3
4
|
/** Vertical script style for a character run. */
|
|
4
5
|
export type CharScript = 'normal' | 'superscript' | 'subscript';
|
|
@@ -124,8 +125,8 @@ export interface MTextToolbarColorPickerContext {
|
|
|
124
125
|
}
|
|
125
126
|
/** Lifecycle hooks for a custom toolbar color picker. */
|
|
126
127
|
export interface MTextToolbarColorPickerInstance {
|
|
127
|
-
/** Sync picker UI when editor format changes. */
|
|
128
|
-
setValue?: (
|
|
128
|
+
/** Sync picker UI when editor format changes (color may be ACI or RGB). */
|
|
129
|
+
setValue?: (color: MTextColor) => void;
|
|
129
130
|
/** Sync picker UI when toolbar theme changes. */
|
|
130
131
|
setTheme?: (theme: MTextToolbarTheme) => void;
|
|
131
132
|
/** Cleanup mounted resources when toolbar is disposed. */
|