@kritzel/engine 0.4.31 → 0.4.33
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.md +49 -49
- package/dist/cjs/{engine.constants-CakmdKMW.js → engine.constants-VyFrUl1W.js} +627 -129
- package/dist/cjs/{index-BbbWU2aN.js → index-BP5Iy6FQ.js} +27 -9
- package/dist/cjs/index.cjs.js +4 -2
- package/dist/cjs/kritzel-awareness-cursors_5.cjs.entry.js +257 -113
- package/dist/cjs/loader.cjs.js +2 -2
- package/dist/cjs/stencil.cjs.js +3 -3
- package/dist/collection/classes/core/core.class.js +40 -32
- package/dist/collection/classes/core/reviver.class.js +4 -0
- package/dist/collection/classes/core/store.class.js +10 -3
- package/dist/collection/classes/managers/cursor.manager.js +10 -5
- package/dist/collection/classes/objects/dynamic-object.class.js +18 -5
- package/dist/collection/classes/objects/iframe.class.js +447 -0
- package/dist/collection/classes/registries/icon-registry.class.js +100 -67
- package/dist/collection/classes/tools/selection-tool.class.js +3 -3
- package/dist/collection/collection-manifest.json +4 -2
- package/dist/collection/components/kritzel-awareness-cursors/kritzel-awareness-cursors.css +115 -115
- package/dist/collection/components/kritzel-awareness-cursors/kritzel-awareness-cursors.js +1 -1
- package/dist/collection/components/kritzel-cursor-trail/kritzel-cursor-trail.css +10 -10
- package/dist/collection/components/kritzel-cursor-trail/kritzel-cursor-trail.js +1 -1
- package/dist/collection/components/kritzel-engine/kritzel-engine.css +80 -80
- package/dist/collection/components/kritzel-engine/kritzel-engine.js +196 -44
- package/dist/collection/components/kritzel-icon/kritzel-icon.css +18 -18
- package/dist/collection/components/kritzel-icon/kritzel-icon.js +25 -4
- package/dist/collection/components/kritzel-watermark/kritzel-watermark.css +29 -29
- package/dist/collection/components/kritzel-watermark/kritzel-watermark.js +2 -2
- package/dist/collection/constants/version.js +1 -1
- package/dist/collection/helpers/cursor.helper.js +8 -8
- package/dist/collection/helpers/object.helper.js +13 -0
- package/dist/collection/helpers/svg-export.helper.js +55 -41
- package/dist/collection/helpers/wheel.helper.js +1 -7
- package/dist/collection/index.js +3 -0
- package/dist/collection/interfaces/embedded-object.interface.js +1 -0
- package/dist/collection/themes/dark-theme.js +1 -5
- package/dist/collection/themes/light-theme.js +1 -6
- package/dist/components/index.js +7 -7
- package/dist/components/kritzel-awareness-cursors.js +1 -1
- package/dist/components/kritzel-cursor-trail.js +1 -1
- package/dist/components/kritzel-engine.js +755 -161
- package/dist/components/kritzel-icon.js +1 -1
- package/dist/components/kritzel-watermark.js +1 -1
- package/dist/components/{p-B2Htv9ou.js → p-BHVbfJrf.js} +3 -3
- package/dist/components/{p-YP8wAcF4.js → p-BTU9qcSW.js} +2 -2
- package/dist/components/{p-CfSx6HU_.js → p-BWfRbEIq.js} +16 -4
- package/dist/components/p-NAEiq8CK.js +214 -0
- package/dist/components/{p-BvRGQvVI.js → p-k0LxV9Bg.js} +18 -14
- package/dist/esm/{engine.constants-Bl5-pbq-.js → engine.constants-BHTr2qPx.js} +626 -130
- package/dist/esm/{index-BU9Mv7y0.js → index-C8PnHbLt.js} +27 -10
- package/dist/esm/index.js +3 -3
- package/dist/esm/kritzel-awareness-cursors_5.entry.js +257 -113
- package/dist/esm/loader.js +3 -3
- package/dist/esm/stencil.js +4 -4
- package/dist/stencil/index.esm.js +3 -3
- package/dist/stencil/{p-f0a8b7af.entry.js → p-40097a38.entry.js} +257 -113
- package/dist/stencil/{p-Bl5-pbq-.js → p-BHTr2qPx.js} +626 -130
- package/dist/stencil/{p-BU9Mv7y0.js → p-C8PnHbLt.js} +27 -10
- package/dist/stencil/stencil.esm.js +4 -4
- package/dist/types/classes/core/store.class.d.ts +3 -2
- package/dist/types/classes/objects/dynamic-object.class.d.ts +4 -2
- package/dist/types/classes/objects/iframe.class.d.ts +76 -0
- package/dist/types/classes/registries/icon-registry.class.d.ts +75 -6
- package/dist/types/components/kritzel-engine/kritzel-engine.d.ts +14 -1
- package/dist/types/components/kritzel-icon/kritzel-icon.d.ts +5 -1
- package/dist/types/components.d.ts +26 -8
- package/dist/types/constants/version.d.ts +1 -1
- package/dist/types/helpers/cursor.helper.d.ts +2 -1
- package/dist/types/helpers/object.helper.d.ts +2 -0
- package/dist/types/helpers/svg-export.helper.d.ts +1 -0
- package/dist/types/helpers/wheel.helper.d.ts +2 -2
- package/dist/types/index.d.ts +3 -0
- package/dist/types/interfaces/context-menu-item.interface.d.ts +2 -1
- package/dist/types/interfaces/dynamic-object-renderer.interface.d.ts +7 -0
- package/dist/types/interfaces/embedded-object.interface.d.ts +9 -0
- package/dist/types/interfaces/engine-state.interface.d.ts +3 -2
- package/dist/types/interfaces/login-config.interface.d.ts +2 -1
- package/dist/types/interfaces/master-detail.interface.d.ts +2 -1
- package/dist/types/interfaces/menu-item.interface.d.ts +2 -1
- package/dist/types/interfaces/theme.interface.d.ts +6 -7
- package/dist/types/interfaces/tool.interface.d.ts +9 -0
- package/dist/types/interfaces/toolbar-item.interface.d.ts +3 -2
- package/dist/types/stencil-public-runtime.d.ts +61 -55
- package/package.json +3 -1
- package/readme.md +10 -10
- package/dist/components/p-fx6msYHJ.js +0 -167
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
import { KritzelMathHelper } from "../../helpers/math.helper";
|
|
2
|
+
import { KritzelBaseObject } from "./base-object.class";
|
|
3
|
+
const IFRAME_STATE_MESSAGE = 'kritzel-iframe-state';
|
|
4
|
+
const IFRAME_PARENT_STATE_MESSAGE = 'kritzel-iframe-parent-state';
|
|
5
|
+
const IFRAME_POINTER_MESSAGE = 'kritzel-iframe-pointer';
|
|
6
|
+
const IFRAME_CONTEXT_MENU_MESSAGE = 'kritzel-iframe-contextmenu';
|
|
7
|
+
const SUPPORTED_SANDBOX_TOKENS = new Set([
|
|
8
|
+
'allow-downloads',
|
|
9
|
+
'allow-forms',
|
|
10
|
+
'allow-modals',
|
|
11
|
+
'allow-orientation-lock',
|
|
12
|
+
'allow-pointer-lock',
|
|
13
|
+
'allow-popups',
|
|
14
|
+
'allow-popups-to-escape-sandbox',
|
|
15
|
+
'allow-presentation',
|
|
16
|
+
'allow-same-origin',
|
|
17
|
+
'allow-scripts',
|
|
18
|
+
'allow-top-navigation-by-user-activation',
|
|
19
|
+
]);
|
|
20
|
+
const iframePointerBridgeStarts = new WeakMap();
|
|
21
|
+
export class KritzelIframeObject extends KritzelBaseObject {
|
|
22
|
+
__class__ = 'KritzelIframeObject';
|
|
23
|
+
content = { html: '', css: '', script: '' };
|
|
24
|
+
loadingContent;
|
|
25
|
+
isLoading = false;
|
|
26
|
+
state = {};
|
|
27
|
+
title = 'Embedded content';
|
|
28
|
+
sandbox = ['allow-scripts'];
|
|
29
|
+
borderRadius;
|
|
30
|
+
boxShadow;
|
|
31
|
+
get html() { return this.content.html; }
|
|
32
|
+
set html(value) { this.content = { ...this.content, html: value }; }
|
|
33
|
+
get css() { return this.content.css; }
|
|
34
|
+
set css(value) { this.content = { ...this.content, css: value }; }
|
|
35
|
+
get script() { return this.content.script; }
|
|
36
|
+
set script(value) { this.content = { ...this.content, script: value }; }
|
|
37
|
+
messageListener = null;
|
|
38
|
+
constructor(config) {
|
|
39
|
+
super();
|
|
40
|
+
this.content = {
|
|
41
|
+
html: config?.content?.html ?? config?.html ?? '',
|
|
42
|
+
css: config?.content?.css ?? config?.css ?? '',
|
|
43
|
+
script: config?.content?.script ?? config?.script ?? '',
|
|
44
|
+
};
|
|
45
|
+
this.loadingContent = config?.loadingContent ? {
|
|
46
|
+
html: config.loadingContent.html ?? '',
|
|
47
|
+
css: config.loadingContent.css ?? '',
|
|
48
|
+
script: config.loadingContent.script ?? '',
|
|
49
|
+
} : undefined;
|
|
50
|
+
this.isLoading = config?.isLoading ?? false;
|
|
51
|
+
this.state = structuredClone(config?.state ?? {});
|
|
52
|
+
this.title = config?.title ?? 'Embedded content';
|
|
53
|
+
this.sandbox = this.normalizeSandbox(config?.sandbox);
|
|
54
|
+
this.borderRadius = config?.borderRadius;
|
|
55
|
+
this.boxShadow = config?.boxShadow;
|
|
56
|
+
this.translateX = config?.translateX ?? 0;
|
|
57
|
+
this.translateY = config?.translateY ?? 0;
|
|
58
|
+
this.rotation = KritzelMathHelper.degreesToRadians(config?.rotation ?? 0);
|
|
59
|
+
this.scale = config?.scale ?? 1;
|
|
60
|
+
this.width = config?.width ?? 0;
|
|
61
|
+
this.height = config?.height ?? 0;
|
|
62
|
+
}
|
|
63
|
+
static create(core, config) {
|
|
64
|
+
const object = new KritzelIframeObject(config);
|
|
65
|
+
object._core = core;
|
|
66
|
+
object.id = object.generateId();
|
|
67
|
+
object.workspaceId = core.store.state.activeWorkspace.id;
|
|
68
|
+
object.userId = core.user?.id;
|
|
69
|
+
object.scale = config?.scale ?? core.store.state.scale;
|
|
70
|
+
object.zIndex = core.store.currentZIndex;
|
|
71
|
+
return object;
|
|
72
|
+
}
|
|
73
|
+
get sandboxAttribute() {
|
|
74
|
+
return this.sandbox.join(' ');
|
|
75
|
+
}
|
|
76
|
+
mount(iframe) {
|
|
77
|
+
if (this.isMounted && this.elementRef === iframe) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (this.isMounted) {
|
|
81
|
+
this.unmount();
|
|
82
|
+
}
|
|
83
|
+
this.elementRef = iframe;
|
|
84
|
+
this.isMounted = true;
|
|
85
|
+
iframe.title = this.title;
|
|
86
|
+
iframe.setAttribute('sandbox', this.sandboxAttribute);
|
|
87
|
+
iframe.style.pointerEvents = this.isInteractive ? 'auto' : 'none';
|
|
88
|
+
this.messageListener = event => this.handleMessage(event);
|
|
89
|
+
window.addEventListener('message', this.messageListener);
|
|
90
|
+
iframe.srcdoc = this.createSourceDocument();
|
|
91
|
+
this.onMount(this.createLifecycleContext());
|
|
92
|
+
}
|
|
93
|
+
unmount() {
|
|
94
|
+
if (!this.isMounted) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const nextState = this.onUnmount(this.createLifecycleContext());
|
|
98
|
+
if (nextState !== undefined) {
|
|
99
|
+
this.state = structuredClone(nextState);
|
|
100
|
+
}
|
|
101
|
+
if (this.messageListener) {
|
|
102
|
+
window.removeEventListener('message', this.messageListener);
|
|
103
|
+
this.messageListener = null;
|
|
104
|
+
}
|
|
105
|
+
this.isMounted = false;
|
|
106
|
+
this._elementRef = null;
|
|
107
|
+
}
|
|
108
|
+
setContent(content) {
|
|
109
|
+
this.content = {
|
|
110
|
+
html: content.html ?? this.content.html,
|
|
111
|
+
css: content.css ?? this.content.css,
|
|
112
|
+
script: content.script ?? this.content.script,
|
|
113
|
+
};
|
|
114
|
+
this.refreshDocument();
|
|
115
|
+
this.persistUpdate();
|
|
116
|
+
}
|
|
117
|
+
setLoadingContent(content) {
|
|
118
|
+
this.loadingContent = {
|
|
119
|
+
html: content.html ?? this.loadingContent?.html ?? '',
|
|
120
|
+
css: content.css ?? this.loadingContent?.css ?? '',
|
|
121
|
+
script: content.script ?? this.loadingContent?.script ?? '',
|
|
122
|
+
};
|
|
123
|
+
this.refreshDocument();
|
|
124
|
+
this.persistUpdate();
|
|
125
|
+
}
|
|
126
|
+
setLoading(isLoading) {
|
|
127
|
+
this.isLoading = isLoading;
|
|
128
|
+
this.refreshDocument();
|
|
129
|
+
this.persistUpdate();
|
|
130
|
+
}
|
|
131
|
+
getActiveContent() {
|
|
132
|
+
const activeLoadingContent = this.isLoading ? this.loadingContent : undefined;
|
|
133
|
+
return {
|
|
134
|
+
html: activeLoadingContent?.html ?? this.content.html,
|
|
135
|
+
css: activeLoadingContent?.css ?? this.content.css,
|
|
136
|
+
script: activeLoadingContent?.script ?? this.content.script,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
setState(state) {
|
|
140
|
+
this.state = structuredClone(state);
|
|
141
|
+
this.elementRef?.contentWindow?.postMessage({ type: IFRAME_PARENT_STATE_MESSAGE, state: this.state }, '*');
|
|
142
|
+
this.persistUpdate();
|
|
143
|
+
}
|
|
144
|
+
setIsInteractive(isInteractive) {
|
|
145
|
+
this.isInteractive = isInteractive;
|
|
146
|
+
if (this.isMounted && this.elementRef) {
|
|
147
|
+
this.elementRef.style.pointerEvents = isInteractive ? 'auto' : 'none';
|
|
148
|
+
if (!isInteractive) {
|
|
149
|
+
this.elementRef.blur();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
hasFocusedInputElement() {
|
|
154
|
+
return this.isInteractive && this.isMounted && document.activeElement === this.elementRef;
|
|
155
|
+
}
|
|
156
|
+
getEmbeddedContentRoots() {
|
|
157
|
+
return this.isMounted && this.elementRef instanceof HTMLElement && this.elementRef.tagName === 'IFRAME' ? [this.elementRef] : [];
|
|
158
|
+
}
|
|
159
|
+
onMount(_context) { }
|
|
160
|
+
onSerialize(context) {
|
|
161
|
+
return context.state;
|
|
162
|
+
}
|
|
163
|
+
onUnmount(context) {
|
|
164
|
+
return context.state;
|
|
165
|
+
}
|
|
166
|
+
serialize() {
|
|
167
|
+
const nextState = this.onSerialize(this.createLifecycleContext());
|
|
168
|
+
this.state = structuredClone(nextState);
|
|
169
|
+
const messageListener = this.messageListener;
|
|
170
|
+
this.messageListener = null;
|
|
171
|
+
try {
|
|
172
|
+
const serialized = super.serialize();
|
|
173
|
+
delete serialized.messageListener;
|
|
174
|
+
return serialized;
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
this.messageListener = messageListener;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
deserialize(object) {
|
|
181
|
+
super.deserialize(object);
|
|
182
|
+
const nextContent = object.content && typeof object.content === 'object' ? object.content : {};
|
|
183
|
+
const nextLoadingContent = object.loadingContent && typeof object.loadingContent === 'object' ? object.loadingContent : undefined;
|
|
184
|
+
this.content = {
|
|
185
|
+
html: typeof nextContent.html === 'string' ? nextContent.html : typeof object.html === 'string' ? object.html : '',
|
|
186
|
+
css: typeof nextContent.css === 'string' ? nextContent.css : typeof object.css === 'string' ? object.css : '',
|
|
187
|
+
script: typeof nextContent.script === 'string' ? nextContent.script : typeof object.script === 'string' ? object.script : '',
|
|
188
|
+
};
|
|
189
|
+
this.loadingContent = nextLoadingContent ? {
|
|
190
|
+
html: typeof nextLoadingContent.html === 'string' ? nextLoadingContent.html : '',
|
|
191
|
+
css: typeof nextLoadingContent.css === 'string' ? nextLoadingContent.css : '',
|
|
192
|
+
script: typeof nextLoadingContent.script === 'string' ? nextLoadingContent.script : '',
|
|
193
|
+
} : undefined;
|
|
194
|
+
this.isLoading = typeof object.isLoading === 'boolean' ? object.isLoading : false;
|
|
195
|
+
this.state = object.state && typeof object.state === 'object' ? structuredClone(object.state) : {};
|
|
196
|
+
this.title = typeof object.title === 'string' ? object.title : 'Embedded content';
|
|
197
|
+
this.sandbox = this.normalizeSandbox(Array.isArray(object.sandbox) ? object.sandbox.filter((token) => typeof token === 'string') : undefined);
|
|
198
|
+
this.borderRadius = (typeof object.borderRadius === 'number' || typeof object.borderRadius === 'string') ? object.borderRadius : undefined;
|
|
199
|
+
this.boxShadow = typeof object.boxShadow === 'string' ? object.boxShadow : undefined;
|
|
200
|
+
this.messageListener = null;
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
copy() {
|
|
204
|
+
const copiedObject = super.copy();
|
|
205
|
+
copiedObject.content = { ...this.content };
|
|
206
|
+
copiedObject.loadingContent = this.loadingContent ? { ...this.loadingContent } : undefined;
|
|
207
|
+
copiedObject.isLoading = this.isLoading;
|
|
208
|
+
copiedObject.state = structuredClone(this.state);
|
|
209
|
+
copiedObject.sandbox = [...this.sandbox];
|
|
210
|
+
copiedObject.borderRadius = this.borderRadius;
|
|
211
|
+
copiedObject.boxShadow = this.boxShadow;
|
|
212
|
+
copiedObject.messageListener = null;
|
|
213
|
+
copiedObject._elementRef = null;
|
|
214
|
+
return copiedObject;
|
|
215
|
+
}
|
|
216
|
+
clone() {
|
|
217
|
+
const clonedObject = super.clone();
|
|
218
|
+
clonedObject.content = { ...this.content };
|
|
219
|
+
clonedObject.loadingContent = this.loadingContent ? { ...this.loadingContent } : undefined;
|
|
220
|
+
clonedObject.isLoading = this.isLoading;
|
|
221
|
+
clonedObject.state = structuredClone(this.state);
|
|
222
|
+
clonedObject.sandbox = [...this.sandbox];
|
|
223
|
+
clonedObject.borderRadius = this.borderRadius;
|
|
224
|
+
clonedObject.boxShadow = this.boxShadow;
|
|
225
|
+
clonedObject.messageListener = null;
|
|
226
|
+
clonedObject.isMounted = false;
|
|
227
|
+
clonedObject._elementRef = null;
|
|
228
|
+
return clonedObject;
|
|
229
|
+
}
|
|
230
|
+
adoptTransientStateFrom(previous) {
|
|
231
|
+
if (!(previous instanceof KritzelIframeObject)) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
this.isInteractive = previous.isInteractive;
|
|
235
|
+
this.isSelected = previous.isSelected;
|
|
236
|
+
this.isHovered = previous.isHovered;
|
|
237
|
+
if (!previous.isMounted || !(previous.elementRef instanceof HTMLElement) || previous.elementRef.tagName !== 'IFRAME') {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (previous.messageListener) {
|
|
241
|
+
window.removeEventListener('message', previous.messageListener);
|
|
242
|
+
}
|
|
243
|
+
this._elementRef = previous.elementRef;
|
|
244
|
+
this.isMounted = true;
|
|
245
|
+
this.messageListener = event => this.handleMessage(event);
|
|
246
|
+
window.addEventListener('message', this.messageListener);
|
|
247
|
+
this.elementRef.style.pointerEvents = this.isInteractive ? 'auto' : 'none';
|
|
248
|
+
previous.messageListener = null;
|
|
249
|
+
previous.isMounted = false;
|
|
250
|
+
previous._elementRef = null;
|
|
251
|
+
}
|
|
252
|
+
createLifecycleContext() {
|
|
253
|
+
return {
|
|
254
|
+
object: this,
|
|
255
|
+
iframe: this.isMounted ? this.elementRef : null,
|
|
256
|
+
state: structuredClone(this.state),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
normalizeSandbox(tokens) {
|
|
260
|
+
const requestedTokens = tokens ?? ['allow-scripts'];
|
|
261
|
+
const normalized = requestedTokens.filter(token => SUPPORTED_SANDBOX_TOKENS.has(token));
|
|
262
|
+
return [...new Set(normalized.length > 0 ? normalized : ['allow-scripts'])];
|
|
263
|
+
}
|
|
264
|
+
createSourceDocument() {
|
|
265
|
+
const activeContent = this.getActiveContent();
|
|
266
|
+
const state = JSON.stringify(this.state).replace(/</g, '\\u003c');
|
|
267
|
+
const css = activeContent.css.replace(/<\/style/gi, '<\\/style');
|
|
268
|
+
const script = activeContent.script.replace(/<\/script/gi, '<\\/script');
|
|
269
|
+
const computedStyle = this.isMounted ? getComputedStyle(this.elementRef) : null;
|
|
270
|
+
const defaultCursor = computedStyle?.getPropertyValue('--kritzel-global-default-cursor').trim() || 'auto';
|
|
271
|
+
const pointerCursor = computedStyle?.getPropertyValue('--kritzel-global-pointer-cursor').trim() || defaultCursor;
|
|
272
|
+
return `<!doctype html>
|
|
273
|
+
<html>
|
|
274
|
+
<head>
|
|
275
|
+
<meta charset="utf-8">
|
|
276
|
+
<style>
|
|
277
|
+
:root {
|
|
278
|
+
--kritzel-iframe-default-cursor: ${defaultCursor};
|
|
279
|
+
--kritzel-iframe-pointer-cursor: ${pointerCursor};
|
|
280
|
+
}
|
|
281
|
+
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; cursor: var(--kritzel-iframe-default-cursor); }
|
|
282
|
+
button, button *, a, a *, input, textarea, select { cursor: var(--kritzel-iframe-pointer-cursor) !important; }
|
|
283
|
+
${css}
|
|
284
|
+
</style>
|
|
285
|
+
</head>
|
|
286
|
+
<body>
|
|
287
|
+
${activeContent.html}
|
|
288
|
+
<script>
|
|
289
|
+
const kritzel = globalThis.kritzel = {
|
|
290
|
+
state: ${state},
|
|
291
|
+
setState(nextState) {
|
|
292
|
+
this.state = nextState;
|
|
293
|
+
parent.postMessage({ type: '${IFRAME_STATE_MESSAGE}', state: nextState }, '*');
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
globalThis.addEventListener('message', event => {
|
|
297
|
+
if (event.data?.type === '${IFRAME_PARENT_STATE_MESSAGE}' && event.data.state && typeof event.data.state === 'object') {
|
|
298
|
+
kritzel.state = event.data.state;
|
|
299
|
+
globalThis.dispatchEvent(new CustomEvent('kritzelstatechange', { detail: kritzel.state }));
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
let bridgedPointerId = null;
|
|
303
|
+
const interactiveSelector = 'button, input, textarea, select, a[href], [contenteditable="true"], [contenteditable=""]';
|
|
304
|
+
const bridgePointerEvent = event => parent.postMessage({
|
|
305
|
+
type: '${IFRAME_POINTER_MESSAGE}',
|
|
306
|
+
eventType: event.type,
|
|
307
|
+
pointerId: event.pointerId,
|
|
308
|
+
pointerType: event.pointerType,
|
|
309
|
+
clientX: event.clientX,
|
|
310
|
+
clientY: event.clientY,
|
|
311
|
+
screenX: event.screenX,
|
|
312
|
+
screenY: event.screenY,
|
|
313
|
+
button: event.button,
|
|
314
|
+
buttons: event.buttons,
|
|
315
|
+
shiftKey: event.shiftKey,
|
|
316
|
+
ctrlKey: event.ctrlKey,
|
|
317
|
+
altKey: event.altKey,
|
|
318
|
+
metaKey: event.metaKey,
|
|
319
|
+
}, '*');
|
|
320
|
+
document.addEventListener('pointerdown', event => {
|
|
321
|
+
if (event.target instanceof Element && event.target.closest(interactiveSelector)) return;
|
|
322
|
+
bridgedPointerId = event.pointerId;
|
|
323
|
+
event.preventDefault();
|
|
324
|
+
bridgePointerEvent(event);
|
|
325
|
+
});
|
|
326
|
+
document.addEventListener('pointermove', event => {
|
|
327
|
+
if (event.pointerId !== bridgedPointerId) return;
|
|
328
|
+
event.preventDefault();
|
|
329
|
+
bridgePointerEvent(event);
|
|
330
|
+
});
|
|
331
|
+
const finishPointerBridge = event => {
|
|
332
|
+
if (event.pointerId !== bridgedPointerId) return;
|
|
333
|
+
event.preventDefault();
|
|
334
|
+
bridgePointerEvent(event);
|
|
335
|
+
bridgedPointerId = null;
|
|
336
|
+
};
|
|
337
|
+
document.addEventListener('pointerup', finishPointerBridge);
|
|
338
|
+
document.addEventListener('pointercancel', finishPointerBridge);
|
|
339
|
+
document.addEventListener('contextmenu', event => {
|
|
340
|
+
event.preventDefault();
|
|
341
|
+
parent.postMessage({
|
|
342
|
+
type: '${IFRAME_CONTEXT_MENU_MESSAGE}',
|
|
343
|
+
clientX: event.clientX,
|
|
344
|
+
clientY: event.clientY,
|
|
345
|
+
button: event.button,
|
|
346
|
+
shiftKey: event.shiftKey,
|
|
347
|
+
ctrlKey: event.ctrlKey,
|
|
348
|
+
altKey: event.altKey,
|
|
349
|
+
metaKey: event.metaKey,
|
|
350
|
+
}, '*');
|
|
351
|
+
});
|
|
352
|
+
${script}
|
|
353
|
+
<\/script>
|
|
354
|
+
</body>
|
|
355
|
+
</html>`;
|
|
356
|
+
}
|
|
357
|
+
handleMessage(event) {
|
|
358
|
+
if (event.source !== this.elementRef?.contentWindow || !event.data || typeof event.data !== 'object') {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const message = event.data;
|
|
362
|
+
if (message.type === IFRAME_POINTER_MESSAGE) {
|
|
363
|
+
this.dispatchPointerMessage(event.data);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (message.type === IFRAME_CONTEXT_MENU_MESSAGE) {
|
|
367
|
+
this.dispatchContextMenuMessage(event.data);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (message.type !== IFRAME_STATE_MESSAGE || !message.state || typeof message.state !== 'object') {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
this.state = structuredClone(message.state);
|
|
374
|
+
this.persistUpdate();
|
|
375
|
+
}
|
|
376
|
+
dispatchPointerMessage(message) {
|
|
377
|
+
if (!this.isMounted || !this.elementRef || !['pointerdown', 'pointermove', 'pointerup', 'pointercancel'].includes(message.eventType)) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const iframeRect = this.elementRef.getBoundingClientRect();
|
|
381
|
+
if (message.eventType === 'pointerdown') {
|
|
382
|
+
const scaleX = this.elementRef.offsetWidth > 0 ? iframeRect.width / this.elementRef.offsetWidth : 1;
|
|
383
|
+
const scaleY = this.elementRef.offsetHeight > 0 ? iframeRect.height / this.elementRef.offsetHeight : 1;
|
|
384
|
+
iframePointerBridgeStarts.set(this.elementRef, {
|
|
385
|
+
pointerId: message.pointerId,
|
|
386
|
+
clientX: iframeRect.left + message.clientX * scaleX,
|
|
387
|
+
clientY: iframeRect.top + message.clientY * scaleY,
|
|
388
|
+
screenX: message.screenX,
|
|
389
|
+
screenY: message.screenY,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
const bridgeStart = iframePointerBridgeStarts.get(this.elementRef);
|
|
393
|
+
const hasBridgeStart = bridgeStart?.pointerId === message.pointerId;
|
|
394
|
+
const clientX = hasBridgeStart ? bridgeStart.clientX + message.screenX - bridgeStart.screenX : iframeRect.left + message.clientX;
|
|
395
|
+
const clientY = hasBridgeStart ? bridgeStart.clientY + message.screenY - bridgeStart.screenY : iframeRect.top + message.clientY;
|
|
396
|
+
this.elementRef.dispatchEvent(new PointerEvent(message.eventType, {
|
|
397
|
+
bubbles: true,
|
|
398
|
+
composed: true,
|
|
399
|
+
cancelable: true,
|
|
400
|
+
pointerId: message.pointerId,
|
|
401
|
+
pointerType: message.pointerType,
|
|
402
|
+
clientX,
|
|
403
|
+
clientY,
|
|
404
|
+
screenX: message.screenX,
|
|
405
|
+
screenY: message.screenY,
|
|
406
|
+
button: message.button,
|
|
407
|
+
buttons: message.buttons,
|
|
408
|
+
shiftKey: message.shiftKey,
|
|
409
|
+
ctrlKey: message.ctrlKey,
|
|
410
|
+
altKey: message.altKey,
|
|
411
|
+
metaKey: message.metaKey,
|
|
412
|
+
}));
|
|
413
|
+
if (message.eventType === 'pointerup' || message.eventType === 'pointercancel') {
|
|
414
|
+
iframePointerBridgeStarts.delete(this.elementRef);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
dispatchContextMenuMessage(message) {
|
|
418
|
+
if (!this.isMounted || !this.elementRef) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const iframeRect = this.elementRef.getBoundingClientRect();
|
|
422
|
+
// Dispatched as a MouseEvent (not PointerEvent) to mirror how browsers
|
|
423
|
+
// natively raise 'contextmenu' for both mouse right-click and touch long-press.
|
|
424
|
+
this.elementRef.dispatchEvent(new MouseEvent('contextmenu', {
|
|
425
|
+
bubbles: true,
|
|
426
|
+
composed: true,
|
|
427
|
+
cancelable: true,
|
|
428
|
+
clientX: iframeRect.left + message.clientX,
|
|
429
|
+
clientY: iframeRect.top + message.clientY,
|
|
430
|
+
button: message.button,
|
|
431
|
+
shiftKey: message.shiftKey,
|
|
432
|
+
ctrlKey: message.ctrlKey,
|
|
433
|
+
altKey: message.altKey,
|
|
434
|
+
metaKey: message.metaKey,
|
|
435
|
+
}));
|
|
436
|
+
}
|
|
437
|
+
refreshDocument() {
|
|
438
|
+
if (this.isMounted && this.elementRef) {
|
|
439
|
+
this.elementRef.srcdoc = this.createSourceDocument();
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
persistUpdate() {
|
|
443
|
+
if (this._core?.store?.objects) {
|
|
444
|
+
this.update();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|