@kubohiroya/turbowarp-plus-editor-code-views 0.0.0-stage → 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/LICENSE +373 -0
- package/README.ja.md +38 -0
- package/README.md +37 -2
- package/dist/asset-edit-session.d.ts +24 -0
- package/dist/asset-edit-session.js +58 -0
- package/dist/block-argument-text.d.ts +39 -0
- package/dist/block-argument-text.js +170 -0
- package/dist/block-drag.d.ts +17 -0
- package/dist/block-drag.js +184 -0
- package/dist/code-extensions.d.ts +38 -0
- package/dist/code-extensions.js +418 -0
- package/dist/code-pane.d.ts +63 -0
- package/dist/code-pane.js +363 -0
- package/dist/custom-extension-fields.d.ts +45 -0
- package/dist/custom-extension-fields.js +144 -0
- package/dist/detached-block-palette-view.d.ts +10 -0
- package/dist/detached-block-palette-view.js +62 -0
- package/dist/detached-block-palette.d.ts +41 -0
- package/dist/detached-block-palette.js +197 -0
- package/dist/dockview-unsaved.d.ts +45 -0
- package/dist/dockview-unsaved.js +123 -0
- package/dist/history.d.ts +57 -0
- package/dist/history.js +85 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +729 -0
- package/dist/native-extension-field.d.ts +26 -0
- package/dist/native-extension-field.js +204 -0
- package/dist/native-field-svg.d.ts +20 -0
- package/dist/native-field-svg.js +156 -0
- package/dist/palette-category-reorder.d.ts +4 -0
- package/dist/palette-category-reorder.js +167 -0
- package/dist/palette-category-search.d.ts +26 -0
- package/dist/palette-category-search.js +74 -0
- package/dist/palette-dropdown-resize.d.ts +19 -0
- package/dist/palette-dropdown-resize.js +135 -0
- package/dist/parent-field-layout.d.ts +2 -0
- package/dist/parent-field-layout.js +90 -0
- package/dist/parent-field-mirror.d.ts +3 -0
- package/dist/parent-field-mirror.js +290 -0
- package/dist/parent-field-resources.d.ts +9 -0
- package/dist/parent-field-resources.js +199 -0
- package/dist/preview-chrome.d.ts +9 -0
- package/dist/preview-chrome.js +78 -0
- package/dist/procedure-editor.d.ts +9 -0
- package/dist/procedure-editor.js +122 -0
- package/dist/procedure-edits.d.ts +16 -0
- package/dist/procedure-edits.js +47 -0
- package/dist/programmatic-extension-field.d.ts +20 -0
- package/dist/programmatic-extension-field.js +69 -0
- package/dist/protocol.d.ts +15 -0
- package/dist/protocol.js +36 -0
- package/dist/script-layout-dialog.d.ts +10 -0
- package/dist/script-layout-dialog.js +93 -0
- package/dist/script-layout-view.d.ts +68 -0
- package/dist/script-layout-view.js +164 -0
- package/dist/script-layout-workspace.d.ts +45 -0
- package/dist/script-layout-workspace.js +55 -0
- package/dist/script-layout.d.ts +17 -0
- package/dist/script-layout.js +25 -0
- package/dist/script-view-menu.d.ts +46 -0
- package/dist/script-view-menu.js +161 -0
- package/dist/sprite-code-windows.d.ts +31 -0
- package/dist/sprite-code-windows.js +180 -0
- package/dist/stage-code-prewarm.d.ts +52 -0
- package/dist/stage-code-prewarm.js +216 -0
- package/dist/target-combobox.d.ts +10 -0
- package/dist/target-combobox.js +92 -0
- package/dist/variable-references.d.ts +1 -0
- package/dist/variable-references.js +87 -0
- package/dist/workspace.d.ts +103 -0
- package/dist/workspace.js +1102 -0
- package/package.json +100 -4
- package/src/asset-edit-session.ts +72 -0
- package/src/block-argument-text.ts +172 -0
- package/src/block-drag.ts +190 -0
- package/src/code-extensions.ts +394 -0
- package/src/code-pane.ts +353 -0
- package/src/custom-extension-fields.ts +154 -0
- package/src/detached-block-palette-view.tsx +97 -0
- package/src/detached-block-palette.ts +218 -0
- package/src/dockview-unsaved.ts +134 -0
- package/src/history.ts +114 -0
- package/src/index.ts +680 -0
- package/src/native-extension-field.ts +201 -0
- package/src/native-field-svg.ts +140 -0
- package/src/palette-category-reorder.ts +155 -0
- package/src/palette-category-search.ts +82 -0
- package/src/palette-dropdown-resize.ts +132 -0
- package/src/parent-field-layout.ts +88 -0
- package/src/parent-field-mirror.ts +260 -0
- package/src/parent-field-resources.ts +179 -0
- package/src/preview-chrome.ts +82 -0
- package/src/procedure-editor.ts +123 -0
- package/src/procedure-edits.ts +46 -0
- package/src/programmatic-extension-field.ts +76 -0
- package/src/protocol.ts +47 -0
- package/src/script-layout-dialog.ts +104 -0
- package/src/script-layout-view.ts +198 -0
- package/src/script-layout-workspace.ts +95 -0
- package/src/script-layout.ts +40 -0
- package/src/script-view-menu.ts +213 -0
- package/src/sprite-code-windows.tsx +182 -0
- package/src/stage-code-prewarm.ts +242 -0
- package/src/styles.css +595 -0
- package/src/target-combobox.tsx +106 -0
- package/src/variable-references.ts +85 -0
- package/src/workspace.ts +1124 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type Size = {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
};
|
|
5
|
+
type Bounds = Size & {
|
|
6
|
+
left: number;
|
|
7
|
+
top: number;
|
|
8
|
+
};
|
|
9
|
+
export declare function attachPaletteDropdownResize(panel: SVGElement, onResize: () => void): {
|
|
10
|
+
readonly size: Size;
|
|
11
|
+
readonly left: number;
|
|
12
|
+
open(id: string): void;
|
|
13
|
+
close(): void;
|
|
14
|
+
place(rect: Bounds | null): void;
|
|
15
|
+
followAnchor(): void;
|
|
16
|
+
contains(target: Node | null): boolean;
|
|
17
|
+
dispose(): void;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const handleSize = 18;
|
|
2
|
+
const storageKey = (id) => `tw-plus-palette-size:${id}`;
|
|
3
|
+
export function attachPaletteDropdownResize(panel, onResize) {
|
|
4
|
+
const doc = panel.ownerDocument;
|
|
5
|
+
const win = doc.defaultView;
|
|
6
|
+
const handle = doc.createElement('div');
|
|
7
|
+
handle.className = 'tw-palette-dropdown-resize-handle';
|
|
8
|
+
handle.setAttribute('data-palette-resize-handle', '');
|
|
9
|
+
handle.setAttribute('aria-hidden', 'true');
|
|
10
|
+
handle.style.width = handle.style.height = `${handleSize}px`;
|
|
11
|
+
handle.hidden = true;
|
|
12
|
+
panel.after(handle);
|
|
13
|
+
const events = new AbortController();
|
|
14
|
+
const capture = { capture: true, signal: events.signal };
|
|
15
|
+
const unsavedSizes = new Map();
|
|
16
|
+
let categoryId = null;
|
|
17
|
+
let preferredSize;
|
|
18
|
+
let bounds;
|
|
19
|
+
let drag;
|
|
20
|
+
let left;
|
|
21
|
+
const readSize = (id) => {
|
|
22
|
+
if (unsavedSizes.has(id))
|
|
23
|
+
return unsavedSizes.get(id);
|
|
24
|
+
try {
|
|
25
|
+
const stored = win.localStorage.getItem(storageKey(id));
|
|
26
|
+
if (!stored)
|
|
27
|
+
return;
|
|
28
|
+
const size = JSON.parse(stored);
|
|
29
|
+
if (size && typeof size.width === 'number' && typeof size.height === 'number' &&
|
|
30
|
+
Number.isFinite(size.width) && size.width > 0 && Number.isFinite(size.height) && size.height > 0) {
|
|
31
|
+
return { width: size.width, height: size.height };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// The saved preference may be malformed or storage unavailable.
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const stopDrag = () => {
|
|
39
|
+
if (!drag)
|
|
40
|
+
return;
|
|
41
|
+
const { pointerId } = drag;
|
|
42
|
+
drag = undefined;
|
|
43
|
+
if (handle.hasPointerCapture(pointerId))
|
|
44
|
+
handle.releasePointerCapture(pointerId);
|
|
45
|
+
};
|
|
46
|
+
handle.addEventListener('pointerdown', event => {
|
|
47
|
+
if (event.button !== 0 || drag || categoryId === null || !bounds)
|
|
48
|
+
return;
|
|
49
|
+
event.preventDefault();
|
|
50
|
+
event.stopPropagation();
|
|
51
|
+
const { width, height } = bounds;
|
|
52
|
+
const start = { width, height };
|
|
53
|
+
drag = { pointerId: event.pointerId, x: event.clientX, y: event.clientY, start, size: start, previousLeft: left };
|
|
54
|
+
left = bounds.left;
|
|
55
|
+
handle.setPointerCapture(event.pointerId);
|
|
56
|
+
}, capture);
|
|
57
|
+
handle.addEventListener('pointermove', event => {
|
|
58
|
+
if (drag?.pointerId !== event.pointerId)
|
|
59
|
+
return;
|
|
60
|
+
const { start, x, y } = drag;
|
|
61
|
+
drag.size = {
|
|
62
|
+
width: Math.max(Math.min(80, start.width), start.width + event.clientX - x),
|
|
63
|
+
height: Math.max(Math.min(64, start.height), start.height + event.clientY - y)
|
|
64
|
+
};
|
|
65
|
+
onResize();
|
|
66
|
+
}, capture);
|
|
67
|
+
handle.addEventListener('pointerup', event => {
|
|
68
|
+
if (drag?.pointerId !== event.pointerId)
|
|
69
|
+
return;
|
|
70
|
+
if (bounds && (drag.start.width !== bounds.width || drag.start.height !== bounds.height)) {
|
|
71
|
+
preferredSize = { width: bounds.width, height: bounds.height };
|
|
72
|
+
try {
|
|
73
|
+
win.localStorage.setItem(storageKey(categoryId), JSON.stringify(preferredSize));
|
|
74
|
+
unsavedSizes.delete(categoryId);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
unsavedSizes.set(categoryId, preferredSize);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else
|
|
81
|
+
left = drag.previousLeft;
|
|
82
|
+
stopDrag();
|
|
83
|
+
onResize();
|
|
84
|
+
}, capture);
|
|
85
|
+
const cancelDrag = (event) => {
|
|
86
|
+
if (drag?.pointerId !== event.pointerId)
|
|
87
|
+
return;
|
|
88
|
+
left = drag.previousLeft;
|
|
89
|
+
stopDrag();
|
|
90
|
+
onResize();
|
|
91
|
+
};
|
|
92
|
+
handle.addEventListener('pointercancel', cancelDrag, capture);
|
|
93
|
+
handle.addEventListener('lostpointercapture', cancelDrag, capture);
|
|
94
|
+
return {
|
|
95
|
+
get size() {
|
|
96
|
+
return drag?.size ?? preferredSize;
|
|
97
|
+
},
|
|
98
|
+
get left() {
|
|
99
|
+
return left;
|
|
100
|
+
},
|
|
101
|
+
open(id) {
|
|
102
|
+
categoryId = id;
|
|
103
|
+
preferredSize = readSize(id);
|
|
104
|
+
},
|
|
105
|
+
close() {
|
|
106
|
+
stopDrag();
|
|
107
|
+
categoryId = null;
|
|
108
|
+
preferredSize = undefined;
|
|
109
|
+
bounds = undefined;
|
|
110
|
+
left = undefined;
|
|
111
|
+
handle.hidden = true;
|
|
112
|
+
},
|
|
113
|
+
place(rect) {
|
|
114
|
+
bounds = rect;
|
|
115
|
+
handle.hidden = !rect || rect.width < handleSize || rect.height < handleSize;
|
|
116
|
+
if (handle.hidden)
|
|
117
|
+
return;
|
|
118
|
+
const x = rect.left + rect.width - handleSize;
|
|
119
|
+
const y = rect.top + rect.height - handleSize;
|
|
120
|
+
handle.style.transform = `translate(${x}px, ${y}px)`;
|
|
121
|
+
},
|
|
122
|
+
followAnchor() {
|
|
123
|
+
if (!drag)
|
|
124
|
+
left = undefined;
|
|
125
|
+
},
|
|
126
|
+
contains(target) {
|
|
127
|
+
return handle.contains(target);
|
|
128
|
+
},
|
|
129
|
+
dispose() {
|
|
130
|
+
stopDrag();
|
|
131
|
+
events.abort();
|
|
132
|
+
handle.remove();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const flexDisplays = new Set(['flex', 'inline-flex']);
|
|
2
|
+
const gridDisplays = new Set(['grid', 'inline-grid']);
|
|
3
|
+
const dimensions = ['width', 'min-width', 'max-width'];
|
|
4
|
+
const align = /^(?:normal|stretch|start|end|self-start|self-end|flex-start|flex-end|center|baseline|auto)$/;
|
|
5
|
+
const distribute = /^(?:normal|stretch|start|end|flex-start|flex-end|left|right|center|space-between|space-around|space-evenly)$/;
|
|
6
|
+
const placement = /^(?:auto|(?:span )?[1-8])$/;
|
|
7
|
+
function pixels(value, limit) {
|
|
8
|
+
return /^\d+(?:\.\d+)?px$/.test(value) && Number.parseFloat(value) <= limit;
|
|
9
|
+
}
|
|
10
|
+
function size(value) {
|
|
11
|
+
return value === 'auto' || pixels(value, 480) ||
|
|
12
|
+
(/^\d+(?:\.\d+)?%$/.test(value) && Number.parseFloat(value) <= 100);
|
|
13
|
+
}
|
|
14
|
+
function tracks(value) {
|
|
15
|
+
if (value === 'none')
|
|
16
|
+
return true;
|
|
17
|
+
const list = value.split(/\s+/);
|
|
18
|
+
return list.length <= 8 && list.every(track => track === 'auto' || pixels(track, 480) ||
|
|
19
|
+
(/^\d+(?:\.\d+)?fr$/.test(track) && Number.parseFloat(track) <= 16));
|
|
20
|
+
}
|
|
21
|
+
/** Recreate the layout using the popup's own dimensions and the mirrored controls. */
|
|
22
|
+
export function createFieldLayoutMirror(original, proxy) {
|
|
23
|
+
let applied = new Set();
|
|
24
|
+
return (computed) => {
|
|
25
|
+
const next = new Set();
|
|
26
|
+
const set = (name, value) => {
|
|
27
|
+
next.add(name);
|
|
28
|
+
if (proxy.style.getPropertyValue(name) !== value)
|
|
29
|
+
proxy.style.setProperty(name, value);
|
|
30
|
+
};
|
|
31
|
+
const copy = (name, accepts, value = computed.getPropertyValue(name)) => {
|
|
32
|
+
if (accepts(value))
|
|
33
|
+
set(name, value);
|
|
34
|
+
};
|
|
35
|
+
const parentDisplay = proxy.parentElement?.style.display;
|
|
36
|
+
const flexItem = flexDisplays.has(parentDisplay);
|
|
37
|
+
const gridItem = gridDisplays.has(parentDisplay);
|
|
38
|
+
const flex = flexDisplays.has(computed.display);
|
|
39
|
+
const grid = gridDisplays.has(computed.display);
|
|
40
|
+
const typed = flex || grid || flexItem || gridItem ? original.computedStyleMap?.() : undefined;
|
|
41
|
+
const computedValue = (name) => typed?.get(name)?.toString() ?? '';
|
|
42
|
+
// The CSSOM resolved track list is measured in the parent's pixels. Keep fr units.
|
|
43
|
+
const columns = grid ? computedValue('grid-template-columns') : '';
|
|
44
|
+
const rows = grid ? computedValue('grid-template-rows') : '';
|
|
45
|
+
const supportedGrid = grid && tracks(columns) && tracks(rows) && computedValue('grid-template-areas') === 'none';
|
|
46
|
+
if (flex || supportedGrid) {
|
|
47
|
+
set('display', computed.display);
|
|
48
|
+
for (const name of ['row-gap', 'column-gap'])
|
|
49
|
+
copy(name, value => value === 'normal' || pixels(value, 64));
|
|
50
|
+
copy('justify-content', value => distribute.test(value));
|
|
51
|
+
copy('align-content', value => distribute.test(value) || value === 'baseline');
|
|
52
|
+
copy('align-items', value => align.test(value));
|
|
53
|
+
}
|
|
54
|
+
if (flex) {
|
|
55
|
+
copy('flex-direction', value => /^(?:row|column)(?:-reverse)?$/.test(value));
|
|
56
|
+
copy('flex-wrap', value => /^(?:nowrap|wrap|wrap-reverse)$/.test(value));
|
|
57
|
+
}
|
|
58
|
+
if (supportedGrid) {
|
|
59
|
+
set('grid-template-columns', columns);
|
|
60
|
+
set('grid-template-rows', rows);
|
|
61
|
+
copy('grid-auto-flow', value => /^(?:row|column)(?: dense)?$/.test(value) || value === 'dense');
|
|
62
|
+
copy('justify-items', value => align.test(value));
|
|
63
|
+
}
|
|
64
|
+
if (flex || supportedGrid || flexItem || gridItem) {
|
|
65
|
+
for (const name of dimensions)
|
|
66
|
+
copy(name, value => size(value) || value === 'none', computedValue(name));
|
|
67
|
+
copy('box-sizing', value => value === 'border-box' || value === 'content-box');
|
|
68
|
+
}
|
|
69
|
+
if (flexItem || gridItem) {
|
|
70
|
+
copy('align-self', value => align.test(value));
|
|
71
|
+
}
|
|
72
|
+
if (flexItem) {
|
|
73
|
+
for (const name of ['flex-grow', 'flex-shrink']) {
|
|
74
|
+
copy(name, value => /^\d+(?:\.\d+)?$/.test(value) && Number(value) <= 16);
|
|
75
|
+
}
|
|
76
|
+
copy('flex-basis', size);
|
|
77
|
+
}
|
|
78
|
+
if (gridItem) {
|
|
79
|
+
copy('justify-self', value => align.test(value));
|
|
80
|
+
for (const axis of ['column', 'row']) {
|
|
81
|
+
copy(`grid-${axis}-start`, value => placement.test(value));
|
|
82
|
+
copy(`grid-${axis}-end`, value => placement.test(value));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for (const name of applied)
|
|
86
|
+
if (!next.has(name))
|
|
87
|
+
proxy.style.removeProperty(name);
|
|
88
|
+
applied = next;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type FieldResourceOptions } from './parent-field-resources.ts';
|
|
2
|
+
/** The parent's widget remains authoritative, including its document-level handlers. */
|
|
3
|
+
export declare function mirrorParentField(source: HTMLElement, host: HTMLElement, valid: () => boolean, options?: FieldResourceOptions): () => void;
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { createFieldLayoutMirror } from "./parent-field-layout.js";
|
|
2
|
+
import { createFieldResources } from "./parent-field-resources.js";
|
|
3
|
+
const htmlNamespace = 'http://www.w3.org/1999/xhtml';
|
|
4
|
+
const elements = new Set(['div', 'span', 'p', 'fieldset', 'legend', 'label', 'textarea', 'input', 'button']);
|
|
5
|
+
const inputTypes = new Set(['text', 'number', 'range']);
|
|
6
|
+
const attributes = ['aria-label', 'title', 'placeholder', 'min', 'max', 'step', 'maxlength'];
|
|
7
|
+
const sides = ['top', 'right', 'bottom', 'left'];
|
|
8
|
+
const colors = ['color', 'background-color', ...sides.map(side => `border-${side}-color`)];
|
|
9
|
+
const lengths = ['font-size', 'line-height', ...sides.flatMap(side => [`padding-${side}`, `border-${side}-width`]),
|
|
10
|
+
'border-top-left-radius', 'border-top-right-radius', 'border-bottom-right-radius', 'border-bottom-left-radius'];
|
|
11
|
+
const borderStyles = sides.map(side => `border-${side}-style`);
|
|
12
|
+
const typography = ['font-family', 'font-style', 'font-weight'];
|
|
13
|
+
const length = /^\d+(?:\.\d+)?px$/;
|
|
14
|
+
const color = /^(?:#[\da-f]{3,8}|[a-z]+|rgba?\([\d.,%\s]+\))$/i;
|
|
15
|
+
const generatedText = /^(?:"[^"\\\x00-\x1f]{0,120}"|none|normal)$/;
|
|
16
|
+
const stateSelector = /^\.([\w-]+)(:hover|:focus|:focus-visible)(::before|::after)?$/;
|
|
17
|
+
const pseudoElements = ['::before', '::after'];
|
|
18
|
+
const animatedColors = colors.map(name => name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()));
|
|
19
|
+
function widgetAnimation(original) {
|
|
20
|
+
const parent = original.ownerDocument.defaultView;
|
|
21
|
+
if (!parent?.CSSAnimation || !parent.KeyframeEffect || !original.getAnimations)
|
|
22
|
+
return;
|
|
23
|
+
const animations = original.getAnimations().filter(animation => animation instanceof parent.CSSAnimation &&
|
|
24
|
+
animation.effect instanceof parent.KeyframeEffect && animation.effect.target === original &&
|
|
25
|
+
!animation.effect.pseudoElement);
|
|
26
|
+
if (animations.length !== 1)
|
|
27
|
+
return;
|
|
28
|
+
const [animation] = animations;
|
|
29
|
+
if (animation.playState !== 'running' || animation.playbackRate !== 1 ||
|
|
30
|
+
animation.timeline !== original.ownerDocument.timeline)
|
|
31
|
+
return;
|
|
32
|
+
const effect = animation.effect;
|
|
33
|
+
const timing = effect.getTiming();
|
|
34
|
+
if (effect.composite !== 'replace' || typeof timing.duration !== 'number' ||
|
|
35
|
+
timing.duration <= 0 || timing.duration > 60000 || Math.abs(timing.delay) > 60000)
|
|
36
|
+
return;
|
|
37
|
+
const source = effect.getKeyframes();
|
|
38
|
+
if (source.length < 2 || source.length > 32 ||
|
|
39
|
+
source.some(frame => frame.composite !== 'auto' && frame.composite !== 'replace'))
|
|
40
|
+
return;
|
|
41
|
+
const accepts = (property, value) => typeof value === 'string' && value.length <= 128 &&
|
|
42
|
+
(property === 'opacity' ? /^\d+(?:\.\d+)?$/.test(value) && Number(value) <= 1 : color.test(value));
|
|
43
|
+
// Require explicit endpoints so the replay does not interpolate from the parent's sampled style.
|
|
44
|
+
const properties = [...animatedColors, 'opacity'].filter(property => source[0].computedOffset === 0 && source.at(-1).computedOffset === 1 &&
|
|
45
|
+
accepts(property, source[0][property]) && accepts(property, source.at(-1)[property]));
|
|
46
|
+
if (!properties.length)
|
|
47
|
+
return;
|
|
48
|
+
const keyframes = source.map(frame => {
|
|
49
|
+
const projected = { offset: frame.computedOffset, easing: frame.easing };
|
|
50
|
+
for (const property of properties) {
|
|
51
|
+
if (accepts(property, frame[property]))
|
|
52
|
+
projected[property] = frame[property];
|
|
53
|
+
}
|
|
54
|
+
return projected;
|
|
55
|
+
});
|
|
56
|
+
return { keyframes, timing };
|
|
57
|
+
}
|
|
58
|
+
function mirrorWidgetAnimations(host, nodes) {
|
|
59
|
+
const motion = host.ownerDocument.defaultView?.matchMedia('(prefers-reduced-motion: reduce)');
|
|
60
|
+
if (!motion || motion.matches)
|
|
61
|
+
return () => { };
|
|
62
|
+
const animations = [];
|
|
63
|
+
for (const [original, proxy] of nodes) {
|
|
64
|
+
const projection = widgetAnimation(original);
|
|
65
|
+
if (projection)
|
|
66
|
+
animations.push(proxy.animate(projection.keyframes, projection.timing));
|
|
67
|
+
}
|
|
68
|
+
const stop = () => {
|
|
69
|
+
motion.removeEventListener('change', stop);
|
|
70
|
+
for (const animation of animations)
|
|
71
|
+
animation.cancel();
|
|
72
|
+
};
|
|
73
|
+
if (animations.length)
|
|
74
|
+
motion.addEventListener('change', stop, { once: true });
|
|
75
|
+
return stop;
|
|
76
|
+
}
|
|
77
|
+
function copyAppearance(source, target, { pseudo = false, priority = '', fontFamily = '' } = {}) {
|
|
78
|
+
const copy = (name, accept) => {
|
|
79
|
+
const value = name === 'font-family' && fontFamily ? fontFamily : source.getPropertyValue(name);
|
|
80
|
+
const safe = value.length <= 128 && accept(value) ? value : '';
|
|
81
|
+
if (target.getPropertyValue(name) !== safe)
|
|
82
|
+
target.setProperty(name, safe, priority);
|
|
83
|
+
};
|
|
84
|
+
for (const name of colors)
|
|
85
|
+
copy(name, value => color.test(value));
|
|
86
|
+
for (const name of lengths)
|
|
87
|
+
copy(name, value => (name === 'line-height' && value === 'normal') ||
|
|
88
|
+
(length.test(value) && Number.parseFloat(value) <= 128));
|
|
89
|
+
for (const name of borderStyles)
|
|
90
|
+
copy(name, value => /^(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)$/.test(value));
|
|
91
|
+
for (const name of typography)
|
|
92
|
+
copy(name, value => {
|
|
93
|
+
if (name === 'font-family')
|
|
94
|
+
return /^[\w ,'"-]+$/.test(value);
|
|
95
|
+
if (name === 'font-style')
|
|
96
|
+
return /^(normal|italic|oblique)$/.test(value);
|
|
97
|
+
return /^(normal|bold|[1-9]00)$/.test(value);
|
|
98
|
+
});
|
|
99
|
+
if (pseudo)
|
|
100
|
+
copy('content', value => generatedText.test(value));
|
|
101
|
+
}
|
|
102
|
+
function readStateRules(document) {
|
|
103
|
+
const rules = [];
|
|
104
|
+
for (const sheet of document.styleSheets) {
|
|
105
|
+
if (sheet.disabled || (sheet.media.mediaText && !document.defaultView?.matchMedia(sheet.media.mediaText).matches))
|
|
106
|
+
continue;
|
|
107
|
+
let source;
|
|
108
|
+
try {
|
|
109
|
+
source = sheet.cssRules;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
continue; // Cross-origin stylesheets are opaque.
|
|
113
|
+
}
|
|
114
|
+
for (const rule of source) {
|
|
115
|
+
if (rule.type !== 1)
|
|
116
|
+
continue;
|
|
117
|
+
const { selectorText, style } = rule;
|
|
118
|
+
const match = stateSelector.exec(selectorText);
|
|
119
|
+
if (match)
|
|
120
|
+
rules.push({ className: match[1], state: match[2], pseudo: match[3] ?? '', style });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return rules;
|
|
124
|
+
}
|
|
125
|
+
function mirrorWidgetStyles(parent, host, nodes) {
|
|
126
|
+
const style = host.ownerDocument.createElement('style');
|
|
127
|
+
host.append(style);
|
|
128
|
+
const sheet = style.sheet;
|
|
129
|
+
if (!sheet)
|
|
130
|
+
return () => { };
|
|
131
|
+
const stateRules = readStateRules(parent.document);
|
|
132
|
+
const refresh = [];
|
|
133
|
+
nodes.forEach(([original, proxy], index) => {
|
|
134
|
+
proxy.dataset.plusNativeFieldNode = String(index);
|
|
135
|
+
const selector = `[data-plus-native-field] [data-plus-native-field-node="${index}"]`;
|
|
136
|
+
const add = (suffix) => {
|
|
137
|
+
const index = sheet.insertRule(`${selector}${suffix} {}`, sheet.cssRules.length);
|
|
138
|
+
return sheet.cssRules[index].style;
|
|
139
|
+
};
|
|
140
|
+
for (const pseudo of pseudoElements) {
|
|
141
|
+
let target;
|
|
142
|
+
refresh.push(() => {
|
|
143
|
+
const computed = parent.getComputedStyle(original, pseudo);
|
|
144
|
+
if (!target && (!generatedText.test(computed.content) || !computed.content.startsWith('"')))
|
|
145
|
+
return;
|
|
146
|
+
target ??= add(pseudo);
|
|
147
|
+
copyAppearance(computed, target, { pseudo: true });
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
for (const { className, state, pseudo, style } of stateRules) {
|
|
151
|
+
if (!original.classList.contains(className))
|
|
152
|
+
continue;
|
|
153
|
+
// Override the inline baseline while letting the child browser resolve the state.
|
|
154
|
+
copyAppearance(style, add(state + pseudo), { pseudo: Boolean(pseudo), priority: 'important' });
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
return () => { for (const update of refresh)
|
|
158
|
+
update(); };
|
|
159
|
+
}
|
|
160
|
+
/** The parent's widget remains authoritative, including its document-level handlers. */
|
|
161
|
+
export function mirrorParentField(source, host, valid, options) {
|
|
162
|
+
const parent = source.ownerDocument.defaultView;
|
|
163
|
+
if (!parent)
|
|
164
|
+
return;
|
|
165
|
+
const sync = [];
|
|
166
|
+
const nodes = [];
|
|
167
|
+
const resources = createFieldResources(source, options, valid);
|
|
168
|
+
const fonts = new WeakMap();
|
|
169
|
+
let remaining = 128;
|
|
170
|
+
let controls = 0;
|
|
171
|
+
function mirror(node, depth) {
|
|
172
|
+
if (remaining <= 0 || depth > 8)
|
|
173
|
+
return;
|
|
174
|
+
remaining--;
|
|
175
|
+
if (node.nodeType === 3) {
|
|
176
|
+
const copy = host.ownerDocument.createTextNode('');
|
|
177
|
+
sync.push(() => { copy.data = (node.textContent ?? '').slice(0, 1024); });
|
|
178
|
+
return copy;
|
|
179
|
+
}
|
|
180
|
+
if (node.nodeType !== 1)
|
|
181
|
+
return;
|
|
182
|
+
const original = node;
|
|
183
|
+
const tag = original.localName;
|
|
184
|
+
if (original.namespaceURI !== htmlNamespace || !(elements.has(tag) || (tag === 'img' && resources.hasImage(original))))
|
|
185
|
+
return;
|
|
186
|
+
if (tag === 'input' && !inputTypes.has(original.type))
|
|
187
|
+
return;
|
|
188
|
+
if (tag === 'button' && original.form && original.type !== 'button')
|
|
189
|
+
return;
|
|
190
|
+
const proxy = host.ownerDocument.createElement(tag);
|
|
191
|
+
const mirrorLayout = createFieldLayoutMirror(original, proxy);
|
|
192
|
+
nodes.push([original, proxy]);
|
|
193
|
+
sync.push(() => {
|
|
194
|
+
proxy.hidden = original.hidden;
|
|
195
|
+
const computed = parent.getComputedStyle(original);
|
|
196
|
+
copyAppearance(computed, proxy.style, { fontFamily: fonts.get(proxy) });
|
|
197
|
+
mirrorLayout(computed);
|
|
198
|
+
});
|
|
199
|
+
for (const name of attributes) {
|
|
200
|
+
const value = original.getAttribute(name);
|
|
201
|
+
if (value !== null)
|
|
202
|
+
proxy.setAttribute(name, value.slice(0, 1024));
|
|
203
|
+
}
|
|
204
|
+
if (tag === 'img') {
|
|
205
|
+
proxy.setAttribute('alt', (original.getAttribute('alt') ?? '').slice(0, 1024));
|
|
206
|
+
for (const name of ['width', 'height']) {
|
|
207
|
+
const size = original.getAttribute(name) ?? '';
|
|
208
|
+
if (/^[1-9]\d{0,2}$/.test(size) && Number(size) <= 512)
|
|
209
|
+
proxy.setAttribute(name, size);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const canRelay = () => valid() && source.contains(original);
|
|
213
|
+
if (tag === 'input' || tag === 'textarea') {
|
|
214
|
+
controls++;
|
|
215
|
+
const control = original;
|
|
216
|
+
const copy = proxy;
|
|
217
|
+
if (tag === 'input')
|
|
218
|
+
copy.type = control.type;
|
|
219
|
+
sync.push(() => {
|
|
220
|
+
copy.disabled = control.disabled;
|
|
221
|
+
copy.readOnly = control.readOnly;
|
|
222
|
+
if (copy.value !== control.value)
|
|
223
|
+
copy.value = control.value;
|
|
224
|
+
});
|
|
225
|
+
for (const type of ['input', 'change']) {
|
|
226
|
+
copy.addEventListener(type, event => {
|
|
227
|
+
if (!canRelay() || control.disabled || control.readOnly)
|
|
228
|
+
return;
|
|
229
|
+
control.value = copy.value;
|
|
230
|
+
const input = event;
|
|
231
|
+
control.dispatchEvent(type === 'input' ? new parent.InputEvent(type, {
|
|
232
|
+
bubbles: true, data: input.data, inputType: input.inputType, isComposing: input.isComposing
|
|
233
|
+
}) : new parent.Event(type, { bubbles: true }));
|
|
234
|
+
refresh();
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
if (tag === 'button') {
|
|
240
|
+
controls++;
|
|
241
|
+
const button = proxy;
|
|
242
|
+
const control = original;
|
|
243
|
+
button.type = 'button';
|
|
244
|
+
sync.push(() => { button.disabled = control.disabled; });
|
|
245
|
+
button.addEventListener('click', () => {
|
|
246
|
+
if (!canRelay() || control.disabled)
|
|
247
|
+
return;
|
|
248
|
+
original.dispatchEvent(new parent.MouseEvent('click', { bubbles: true, cancelable: true }));
|
|
249
|
+
refresh();
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (tag === 'fieldset')
|
|
253
|
+
sync.push(() => {
|
|
254
|
+
proxy.disabled = original.disabled;
|
|
255
|
+
});
|
|
256
|
+
for (const child of original.childNodes) {
|
|
257
|
+
const copy = mirror(child, depth + 1);
|
|
258
|
+
if (copy)
|
|
259
|
+
proxy.append(copy);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return proxy;
|
|
263
|
+
}
|
|
264
|
+
function refresh() {
|
|
265
|
+
for (const update of sync)
|
|
266
|
+
update();
|
|
267
|
+
}
|
|
268
|
+
for (const node of source.childNodes) {
|
|
269
|
+
const copy = mirror(node, 0);
|
|
270
|
+
if (copy)
|
|
271
|
+
host.append(copy);
|
|
272
|
+
}
|
|
273
|
+
if (!controls)
|
|
274
|
+
return;
|
|
275
|
+
sync.push(mirrorWidgetStyles(parent, host, nodes));
|
|
276
|
+
refresh();
|
|
277
|
+
const stopAnimations = mirrorWidgetAnimations(host, nodes);
|
|
278
|
+
for (const [original, proxy] of nodes) {
|
|
279
|
+
void resources.attach(original, proxy)?.then(family => {
|
|
280
|
+
if (!family || !valid())
|
|
281
|
+
return;
|
|
282
|
+
fonts.set(proxy, family);
|
|
283
|
+
proxy.style.fontFamily = family;
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return () => {
|
|
287
|
+
resources.dispose();
|
|
288
|
+
stopAnimations();
|
|
289
|
+
};
|
|
290
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type FieldResourceOptions = {
|
|
2
|
+
manifest: unknown;
|
|
3
|
+
canFetch(url: string): boolean | Promise<boolean>;
|
|
4
|
+
};
|
|
5
|
+
export declare function createFieldResources(source: HTMLElement, options: FieldResourceOptions | undefined, valid: () => boolean): {
|
|
6
|
+
hasImage(node: HTMLElement): boolean;
|
|
7
|
+
attach(original: HTMLElement, proxy: HTMLElement): Promise<string>;
|
|
8
|
+
dispose(): void;
|
|
9
|
+
};
|