@gramlot/gramlot 0.3.24 → 0.3.27
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/dist/components/chat/__init__.py +1 -1
- package/dist/components/chat/assets/collection.js +3 -3
- package/dist/components/chat/assets/index.js +1 -1
- package/dist/components/editors/__init__.py +2 -2
- package/dist/components/editors/assets/collection.js +21 -20
- package/dist/components/editors/assets/index.js +1 -1
- package/dist/components/editors/assets/modules/chunk-74O4WKIV.js +1 -0
- package/dist/components/editors/assets/modules/{code-mirror-engine-QX4A5FVO.js → code-mirror-engine-RWQTR5G5.js} +29 -29
- package/dist/components/editors/assets/modules/{jodit-engine-MMSPIWM7.js → jodit-engine-MTSA4GZ3.js} +292 -292
- package/dist/components/editors/assets/modules/markdown-document-VFC23FAY.js +1 -0
- package/dist/components/editors/assets/modules/{prose-mirror-engine-IXTRTG6B.js → prose-mirror-engine-GMZPJPNN.js} +13 -13
- package/dist/components/grid/__init__.py +3 -2
- package/dist/components/grid/assets/collection.js +77 -17
- package/dist/components/grid/assets/index.js +1 -1
- package/dist/components/grid/grammar.json +78 -1
- package/dist/components/grouplets/__init__.py +2 -2
- package/dist/components/grouplets/assets/collection.js +3 -20
- package/dist/components/grouplets/assets/index.js +1 -1
- package/dist/components/grouplets/assets/modules/chunk-BLMUM44K.js +1 -0
- package/dist/components/grouplets/assets/modules/markdown-document-YWNZTOKK.js +1 -0
- package/dist/components/inputs/__init__.py +1 -1
- package/dist/components/inputs/assets/collection.js +59 -34
- package/dist/components/inputs/assets/index.js +1 -1
- package/dist/components/inputs/grammar.json +64 -0
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/collection.js +67 -57
- package/dist/components/layout/assets/index.js +1 -1
- package/dist/components/layout/grammar.json +35 -0
- package/dist/components/selection_trial/__init__.py +1 -1
- package/dist/components/selection_trial/assets/collection.js +3 -3
- package/dist/components/selection_trial/assets/index.js +1 -1
- package/dist/components/selection_trial/grammar.json +35 -0
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/collection.js +26 -23
- package/dist/components/tree/assets/index.js +1 -1
- package/dist/components/tree/grammar.json +341 -1
- package/dist/components/typst/__init__.py +1 -1
- package/dist/components/typst/assets/collection.js +3 -3
- package/dist/components/typst/assets/index.js +1 -1
- package/dist/components/uploads/__init__.py +13 -0
- package/dist/components/uploads/assets/collection.js +67 -0
- package/dist/components/uploads/assets/index.js +3 -0
- package/dist/components/uploads/commands.json +7 -0
- package/dist/components/uploads/grammar.json +789 -0
- package/dist/{components/editors/assets/modules/chunk-OCFLP2K4.js → gramlot-markdown.js} +12 -12
- package/dist/gramlot.js +5455 -573
- package/dist/gramlot.min.js +1588 -43
- package/dist/runtime-notices.json +28 -0
- package/package.json +4 -2
- package/src/binding/grid-struct.js +1 -1
- package/src/binding/providers.js +4 -1
- package/src/binding/remote-resolver.js +1 -8
- package/src/binding/runtime.js +40 -1
- package/src/binding/validation-context.js +41 -0
- package/src/browser-camera-provider.d.ts +8 -0
- package/src/browser-camera-provider.js +104 -0
- package/src/builder/source.js +13 -2
- package/src/components/attachment-manager.js +160 -0
- package/src/components/bases.js +19 -6
- package/src/components/border-container.js +86 -8
- package/src/components/buttons.js +11 -1
- package/src/components/choice-editor.js +44 -16
- package/src/components/database-choices.js +13 -8
- package/src/components/editing-status-styles.js +26 -0
- package/src/components/editing-status.js +115 -0
- package/src/components/field-state.js +44 -58
- package/src/components/grid-cell-editor.js +245 -0
- package/src/components/grid-grammar.json +42 -0
- package/src/components/grid-structure.js +5 -1
- package/src/components/grid-styles.js +60 -1
- package/src/components/grid.js +103 -11
- package/src/components/grouplet-card.js +11 -3
- package/src/components/html-preview.js +17 -4
- package/src/components/image-crop-dialog.js +272 -0
- package/src/components/image-crop.js +79 -0
- package/src/components/image-uploader.js +252 -0
- package/src/components/inputs.js +4 -3
- package/src/components/jodit-engine.js +1 -1
- package/src/components/markdown-document.js +2 -2
- package/src/components/parameters.js +6 -2
- package/src/components/screen-capture.js +43 -0
- package/src/components/selected-container.js +6 -2
- package/src/components/tree.js +417 -17
- package/src/components/typed-editor.js +19 -9
- package/src/components/upload-engine.js +156 -0
- package/src/components/uploader.js +208 -0
- package/src/components/uploads.js +7 -0
- package/src/components/validation.js +1 -1
- package/src/editing-session.d.ts +26 -0
- package/src/editing-session.js +494 -0
- package/src/editor-values.js +57 -0
- package/src/experiments/selection-trial.js +18 -2
- package/src/format.js +22 -7
- package/src/gramlot.d.ts +3 -0
- package/src/gramlot.js +9 -5
- package/src/handlers/device-handler.d.ts +2 -1
- package/src/handlers/rpc-handler.d.ts +3 -3
- package/src/index-page-styles.js +1 -0
- package/src/index.d.ts +14 -1
- package/src/index.js +13 -0
- package/src/inout.d.ts +2 -0
- package/src/inout.js +6 -0
- package/src/markdown-loader.d.ts +1 -0
- package/src/markdown-loader.js +4 -0
- package/src/markdown.d.ts +1 -0
- package/src/markdown.js +2 -0
- package/src/media-frame.js +9 -0
- package/src/renderer/attributes.js +7 -1
- package/src/renderer/gramlot-renderer.d.ts +3 -0
- package/src/renderer/gramlot-renderer.js +42 -2
- package/src/server/assets.d.ts +1 -1
- package/src/server/assets.js +1 -1
- package/src/server/gramlot-server.d.ts +8 -1
- package/src/server/gramlot-server.js +83 -7
- package/src/server/index.d.ts +2 -0
- package/src/server/index.js +2 -0
- package/src/server/page-base.js +17 -4
- package/src/server/page.d.ts +13 -1
- package/src/server/page.js +6 -0
- package/src/server/source-import.d.ts +2 -0
- package/src/server/source-import.js +35 -0
- package/src/server/upload.d.ts +20 -0
- package/src/server/upload.js +142 -0
- package/src/server/visual-editor.d.ts +11 -0
- package/src/server/visual-editor.js +159 -0
- package/src/source-picker.d.ts +9 -0
- package/src/source-picker.js +56 -0
- package/src/tree-store.d.ts +24 -0
- package/src/tree-store.js +193 -0
- package/src/validation-run.js +50 -0
- package/src/view/button.js +8 -5
- package/src/view/controls.js +9 -42
- package/src/view/html.js +10 -2
- package/src/visual-editor-canvas.js +29 -0
- package/src/visual-editor-catalogue.js +34 -0
- package/src/visual-editor-icons.js +10 -0
- package/src/visual-editor.d.ts +61 -0
- package/src/visual-editor.js +1062 -0
- package/themes/gramlot-base/theme.css +141 -0
- package/tools/gramlot-tools-inspector.js +1 -1
- package/tools/inspector-agent.js +1 -1
- package/dist/components/editors/assets/modules/chunk-MMJXUHRQ.js +0 -3
- package/src/form-state.d.ts +0 -20
- package/src/form-state.js +0 -198
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/** Private, reusable framing dialog. It returns a Blob and never replaces the source. */
|
|
2
|
+
import {ImageCropSession} from './image-crop.js';
|
|
3
|
+
|
|
4
|
+
export class ImageCropDialog {
|
|
5
|
+
#document; #image; #session; #options; #dialog; #frameImage; #minZoom; #maxZoom;
|
|
6
|
+
#resolve; #settled = false; #events; #frame; #points = new Map(); #gesture; #trackpadGesture;
|
|
7
|
+
constructor({state, document, original, image, sourceWidth, sourceHeight, width, height,
|
|
8
|
+
x = 0, y = 0, zoom = 1, rotate = 0, outputWidth, outputHeight, type, quality}) {
|
|
9
|
+
this.#document = document;
|
|
10
|
+
this.#image = image;
|
|
11
|
+
this.#options = {width,height,sourceWidth,sourceHeight,outputWidth,outputHeight,type,quality};
|
|
12
|
+
this.#minZoom = zoom/5; this.#maxZoom = zoom*5;
|
|
13
|
+
this.#session = new ImageCropSession({state,document,original,image,sourceWidth,sourceHeight,width,height,x,y,zoom,rotate});
|
|
14
|
+
const dialog = this.#dialog = document.createElement('dialog');
|
|
15
|
+
dialog.setAttribute('aria-label', 'Edit image');
|
|
16
|
+
dialog.className = 'gramlot-image-crop-dialog';
|
|
17
|
+
const style = document.createElement('style');
|
|
18
|
+
style.textContent = `
|
|
19
|
+
.gramlot-image-crop-dialog{box-sizing:border-box;width:min(100% - 2rem, 510px);max-width:calc(100vw - 2rem);max-height:calc(100vh - 2rem);
|
|
20
|
+
border:1px solid var(--gramlot-border,#d5dde6);border-radius:18px;padding:0;
|
|
21
|
+
color:var(--gramlot-ink,#202a35);background:var(--gramlot-surface,#fff);
|
|
22
|
+
box-shadow:0 24px 64px #14243a42;font:14px/1.4 system-ui,sans-serif}
|
|
23
|
+
.gramlot-image-crop-dialog::backdrop{background:#152335a6}
|
|
24
|
+
.gramlot-image-crop-dialog *{box-sizing:border-box}
|
|
25
|
+
.gramlot-image-crop-dialog .crop-heading{padding:22px 24px 16px}
|
|
26
|
+
.gramlot-image-crop-dialog h2{font-size:1.18rem;line-height:1.2;margin:0 0 4px;letter-spacing:-.02em}
|
|
27
|
+
.gramlot-image-crop-dialog .crop-subtitle{margin:0;color:var(--gramlot-text-secondary,#62748a);font-size:.9em}
|
|
28
|
+
.gramlot-image-crop-dialog .crop-body{padding:0 24px 20px}
|
|
29
|
+
.gramlot-image-crop-dialog .crop-position{display:grid;grid-template-columns:minmax(0,1fr) 34px;gap:8px;align-items:stretch;margin:0 auto;max-width:100%}
|
|
30
|
+
.gramlot-image-crop-dialog .crop-viewport{position:relative;overflow:auto;max-width:100%;border-radius:10px;background:#e8edf3;
|
|
31
|
+
box-shadow:0 0 0 1px #1f38511a}
|
|
32
|
+
.gramlot-image-crop-dialog .crop-viewport [aria-label="Crop preview"]:focus-visible{outline:2px solid var(--gramlot-focus-ring,#3766b5);outline-offset:-2px}
|
|
33
|
+
.gramlot-image-crop-dialog .crop-handles{display:flex;flex-direction:column;align-items:center;justify-content:space-between}
|
|
34
|
+
.gramlot-image-crop-dialog .crop-handle{width:32px;height:32px;padding:0;display:grid;place-items:center;
|
|
35
|
+
border:1px solid transparent;border-radius:8px;background:transparent;color:var(--gramlot-text-secondary,#62748a);
|
|
36
|
+
opacity:.7;font-size:21px;line-height:1;touch-action:none}
|
|
37
|
+
.gramlot-image-crop-dialog .crop-handle:hover,.gramlot-image-crop-dialog .crop-handle:focus-visible{
|
|
38
|
+
opacity:1;background:var(--gramlot-field-background,#f2f5f9);color:var(--gramlot-action,#3766b5)}
|
|
39
|
+
.gramlot-image-crop-dialog .crop-handle:focus-visible{outline:2px solid var(--gramlot-focus-ring,#3766b5);outline-offset:2px}
|
|
40
|
+
.gramlot-image-crop-dialog .crop-rotate-handle{cursor:grab}
|
|
41
|
+
.gramlot-image-crop-dialog .crop-zoom-handle{cursor:nwse-resize}
|
|
42
|
+
.gramlot-image-crop-dialog .crop-help{margin:0 auto 0 0;flex:1 1 125px;color:var(--gramlot-text-secondary,#7c8793);
|
|
43
|
+
font-size:11px;font-style:italic;font-weight:400;line-height:1.35}
|
|
44
|
+
.gramlot-image-crop-dialog .crop-actions{display:flex;align-items:center;flex-wrap:wrap;gap:8px;padding:12px 24px;
|
|
45
|
+
border-top:1px solid var(--gramlot-border,#e2e8ef)}
|
|
46
|
+
.gramlot-image-crop-dialog .crop-action-buttons{display:flex;gap:6px;margin-left:auto}
|
|
47
|
+
.gramlot-image-crop-dialog button{font:inherit;font-size:12px;line-height:1.2;font-weight:500;cursor:pointer;padding:.32rem .6rem;border-radius:6px;
|
|
48
|
+
border:1px solid var(--gramlot-border-control,#c3cdd9);background:var(--gramlot-button-background,#fff);color:var(--gramlot-button-color,#27384b)}
|
|
49
|
+
.gramlot-image-crop-dialog button.primary{background:var(--gramlot-action,#3766b5);border-color:var(--gramlot-action,#3766b5);color:#fff}
|
|
50
|
+
.gramlot-image-crop-dialog button:disabled{opacity:.55;cursor:default}
|
|
51
|
+
.gramlot-image-crop-dialog [role=alert]:not(:empty){color:var(--gramlot-error,#ae2020);margin-top:.75rem}
|
|
52
|
+
@media(max-width:380px){.gramlot-image-crop-dialog{width:calc(100vw - 8px);max-width:calc(100vw - 8px)}
|
|
53
|
+
.gramlot-image-crop-dialog .crop-heading,.gramlot-image-crop-dialog .crop-body{padding-left:8px;padding-right:8px}
|
|
54
|
+
.gramlot-image-crop-dialog .crop-actions{padding:12px 8px}}
|
|
55
|
+
`;
|
|
56
|
+
const heading = document.createElement('header'); heading.className = 'crop-heading';
|
|
57
|
+
const title = document.createElement('h2'); title.textContent = 'Edit image';
|
|
58
|
+
const subtitle = document.createElement('p'); subtitle.className = 'crop-subtitle'; subtitle.textContent = 'Move and adjust the image within the frame.';
|
|
59
|
+
heading.append(title,subtitle);
|
|
60
|
+
const frame = this.#frame = document.createElement('div');
|
|
61
|
+
frame.setAttribute('aria-label', 'Crop preview'); frame.setAttribute('role','group'); frame.tabIndex = 0;
|
|
62
|
+
frame.setAttribute('aria-description','Use arrow keys to move the image. Use the adjacent controls to zoom or rotate.');
|
|
63
|
+
Object.assign(frame.style, {position:'relative',overflow:'hidden',width:`${width}px`,height:`${height}px`,
|
|
64
|
+
maxWidth:'none',background:'#ddd',margin:'auto',touchAction:'none',userSelect:'none',cursor:'grab'});
|
|
65
|
+
this.#frameImage = document.createElement('img');
|
|
66
|
+
this.#frameImage.src = image.src;
|
|
67
|
+
this.#frameImage.alt = ''; this.#frameImage.draggable = false;
|
|
68
|
+
Object.assign(this.#frameImage.style, {position:'absolute',left:'50%',top:'50%',maxWidth:'none',
|
|
69
|
+
width:`${sourceWidth}px`,height:`${sourceHeight}px`,transformOrigin:'center center'});
|
|
70
|
+
frame.append(this.#frameImage);
|
|
71
|
+
const handle = (mode,caption,glyph) => {
|
|
72
|
+
const button = document.createElement('button'); button.type = 'button'; button.className = `crop-handle crop-${mode}-handle`;
|
|
73
|
+
button.dataset.cropHandle = mode; button.setAttribute('aria-label',caption); button.title = caption; button.textContent = glyph;
|
|
74
|
+
return button;
|
|
75
|
+
};
|
|
76
|
+
const rotateHandle = handle('rotate','Rotate image','↻');
|
|
77
|
+
const zoomHandle = handle('zoom','Zoom image','↘');
|
|
78
|
+
const viewport = document.createElement('div'); viewport.className = 'crop-viewport'; viewport.append(frame);
|
|
79
|
+
const position = document.createElement('div'); position.className = 'crop-position';
|
|
80
|
+
position.style.width = `${width + 42}px`;
|
|
81
|
+
const handles = document.createElement('div'); handles.className = 'crop-handles';
|
|
82
|
+
handles.append(rotateHandle,zoomHandle); position.append(viewport,handles);
|
|
83
|
+
const hint = document.createElement('p'); hint.className = 'crop-help';
|
|
84
|
+
hint.textContent = 'Drag to move · pinch to zoom';
|
|
85
|
+
const body = document.createElement('div'); body.className = 'crop-body'; body.append(position);
|
|
86
|
+
const error = document.createElement('div'); error.setAttribute('role','alert');
|
|
87
|
+
const actions = document.createElement('div'); actions.className = 'crop-actions';
|
|
88
|
+
const reset = document.createElement('button'); reset.type = 'button'; reset.textContent = 'Reset';
|
|
89
|
+
const cancel = document.createElement('button'); cancel.type = 'button'; cancel.textContent = 'Cancel';
|
|
90
|
+
const confirm = document.createElement('button'); confirm.type = 'button'; confirm.textContent = 'Confirm'; confirm.className = 'primary';
|
|
91
|
+
const buttons = document.createElement('div'); buttons.className = 'crop-action-buttons'; buttons.append(reset,cancel,confirm);
|
|
92
|
+
actions.append(hint,buttons); body.append(error); dialog.append(style,heading,body,actions);
|
|
93
|
+
this.#events = new document.defaultView.AbortController();
|
|
94
|
+
const listen = {signal:this.#events.signal};
|
|
95
|
+
frame.addEventListener('pointerdown', event => this.#down(event), listen);
|
|
96
|
+
for (const handle of [rotateHandle,zoomHandle]) handle.addEventListener('pointerdown', event => this.#down(event), listen);
|
|
97
|
+
frame.addEventListener('pointermove', event => this.#move(event), listen);
|
|
98
|
+
frame.addEventListener('pointerup', event => this.#up(event), listen);
|
|
99
|
+
for (const type of ['pointercancel','lostpointercapture']) frame.addEventListener(type, event => {
|
|
100
|
+
if (this.#points.has(event.pointerId)) this.#cancelGesture();
|
|
101
|
+
}, listen);
|
|
102
|
+
frame.addEventListener('dragstart', event => event.preventDefault(), listen);
|
|
103
|
+
frame.addEventListener('wheel', event => this.#wheel(event), {...listen,passive:false});
|
|
104
|
+
frame.addEventListener('gesturestart', event => this.#trackpadStart(event), {...listen,passive:false});
|
|
105
|
+
frame.addEventListener('gesturechange', event => this.#trackpadChange(event), {...listen,passive:false});
|
|
106
|
+
frame.addEventListener('gestureend', event => this.#trackpadEnd(event), {...listen,passive:false});
|
|
107
|
+
document.defaultView.addEventListener('blur', () => this.#cancelGesture(), listen);
|
|
108
|
+
document.addEventListener('visibilitychange', () => { if (document.hidden) this.#cancelGesture(); }, listen);
|
|
109
|
+
frame.addEventListener('keydown', event => {
|
|
110
|
+
const delta = {ArrowLeft:[-1,0],ArrowRight:[1,0],ArrowUp:[0,-1],ArrowDown:[0,1]}[event.key];
|
|
111
|
+
if (!delta) return;
|
|
112
|
+
event.preventDefault(); this.#cancelGesture();
|
|
113
|
+
const value = this.#session.transform, step = event.shiftKey ? 10 : 1;
|
|
114
|
+
value.x += delta[0]*step; value.y += delta[1]*step;
|
|
115
|
+
this.#session.setTransform(value); this.#paint();
|
|
116
|
+
}, listen);
|
|
117
|
+
for (const [button,mode] of [[rotateHandle,'rotate'],[zoomHandle,'zoom']]) button.addEventListener('keydown', event => {
|
|
118
|
+
if (!['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(event.key)) return;
|
|
119
|
+
event.preventDefault(); this.#cancelGesture();
|
|
120
|
+
const positive = event.key === 'ArrowUp' || event.key === 'ArrowRight';
|
|
121
|
+
const value = this.#session.transform;
|
|
122
|
+
if (mode === 'rotate') value.rotate += positive ? 5 : -5;
|
|
123
|
+
else value.zoom = this.#boundedZoom(value.zoom * (positive ? 1.05 : 1/1.05));
|
|
124
|
+
this.#session.setTransform(value); this.#paint();
|
|
125
|
+
}, listen);
|
|
126
|
+
reset.addEventListener('click', () => { this.#cancelGesture(); this.#session.reset(); this.#paint(); }, listen);
|
|
127
|
+
cancel.addEventListener('click', () => this.cancel(), listen);
|
|
128
|
+
dialog.addEventListener('cancel', event => { event.preventDefault(); this.cancel(); }, listen);
|
|
129
|
+
dialog.addEventListener('close', () => { if (!this.#settled) this.cancel(); }, listen);
|
|
130
|
+
confirm.addEventListener('click', async () => {
|
|
131
|
+
this.#cancelGesture(); confirm.disabled = true; error.textContent = '';
|
|
132
|
+
try {
|
|
133
|
+
const transform = this.#session.transform;
|
|
134
|
+
const {outputWidth,outputHeight,type,quality} = this.#options;
|
|
135
|
+
const blob = await this.#session.confirm({outputWidth,outputHeight,type,quality});
|
|
136
|
+
this.#finish({blob,transform});
|
|
137
|
+
} catch (failure) {
|
|
138
|
+
if (!this.#settled) { error.textContent = failure.message; confirm.disabled = false; }
|
|
139
|
+
}
|
|
140
|
+
}, listen);
|
|
141
|
+
this.#paint();
|
|
142
|
+
}
|
|
143
|
+
#geometry() {
|
|
144
|
+
const [a,b] = this.#points.values();
|
|
145
|
+
if (!b) return {x:a.x,y:a.y,distance:0,angle:0};
|
|
146
|
+
return {x:(a.x+b.x)/2,y:(a.y+b.y)/2,distance:Math.hypot(b.x-a.x,b.y-a.y),angle:Math.atan2(b.y-a.y,b.x-a.x)};
|
|
147
|
+
}
|
|
148
|
+
#rebase(mode = 'move') {
|
|
149
|
+
const geometry = this.#geometry();
|
|
150
|
+
if (this.#points.size === 2 && geometry.distance === 0) { this.#cancelGesture(); return; }
|
|
151
|
+
const rect = this.#frame.getBoundingClientRect();
|
|
152
|
+
const center = {x:rect.left+rect.width/2,y:rect.top+rect.height/2};
|
|
153
|
+
const angle = Math.atan2(geometry.y-center.y,geometry.x-center.x);
|
|
154
|
+
const distance = Math.hypot(geometry.x-center.x,geometry.y-center.y);
|
|
155
|
+
this.#gesture = {base:this.#session.transform,geometry,mode,center,distance,lastAngle:geometry.angle,handleAngle:angle,rotation:0};
|
|
156
|
+
}
|
|
157
|
+
#down(event) {
|
|
158
|
+
if (event.button !== 0 || this.#settled || this.#trackpadGesture) return;
|
|
159
|
+
if (this.#points.size && (event.pointerType !== 'touch' || [...this.#points.values()].some(point => point.type !== 'touch'))) return;
|
|
160
|
+
if (this.#points.size === 2) { this.#cancelGesture(); return; }
|
|
161
|
+
const handleMode = event.target.closest('[data-crop-handle]')?.dataset.cropHandle;
|
|
162
|
+
const mode = handleMode ? `handle-${handleMode}` : event.pointerType === 'touch' ? 'move' :
|
|
163
|
+
event.shiftKey && !event.altKey && !event.ctrlKey && !event.metaKey ? 'zoom' :
|
|
164
|
+
event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey ? 'rotate' :
|
|
165
|
+
!event.shiftKey && !event.altKey && !event.ctrlKey && !event.metaKey ? 'move' : null;
|
|
166
|
+
if (!mode) return;
|
|
167
|
+
event.preventDefault();
|
|
168
|
+
this.#points.set(event.pointerId,{x:event.clientX,y:event.clientY,type:event.pointerType});
|
|
169
|
+
this.#frame.setPointerCapture(event.pointerId);
|
|
170
|
+
this.#frame.style.cursor = 'grabbing';
|
|
171
|
+
this.#rebase(mode);
|
|
172
|
+
}
|
|
173
|
+
#move(event) {
|
|
174
|
+
const point = this.#points.get(event.pointerId);
|
|
175
|
+
if (!point || !this.#gesture) return;
|
|
176
|
+
event.preventDefault(); point.x = event.clientX; point.y = event.clientY;
|
|
177
|
+
const current = this.#geometry(), gesture = this.#gesture;
|
|
178
|
+
const dx = current.x-gesture.geometry.x, dy = current.y-gesture.geometry.y;
|
|
179
|
+
const value = {...gesture.base};
|
|
180
|
+
if (this.#points.size === 2) {
|
|
181
|
+
value.x += dx; value.y += dy;
|
|
182
|
+
if (current.distance > 0) {
|
|
183
|
+
const delta = current.angle-gesture.lastAngle;
|
|
184
|
+
gesture.rotation += Math.atan2(Math.sin(delta),Math.cos(delta))*180/Math.PI;
|
|
185
|
+
gesture.lastAngle = current.angle;
|
|
186
|
+
}
|
|
187
|
+
value.rotate += gesture.rotation;
|
|
188
|
+
value.zoom = gesture.base.zoom * current.distance / gesture.geometry.distance;
|
|
189
|
+
} else if (gesture.mode === 'handle-zoom') {
|
|
190
|
+
const distance = Math.hypot(current.x-gesture.center.x,current.y-gesture.center.y);
|
|
191
|
+
if (gesture.distance > 0) value.zoom = gesture.base.zoom * distance/gesture.distance;
|
|
192
|
+
} else if (gesture.mode === 'handle-rotate') {
|
|
193
|
+
const angle = Math.atan2(current.y-gesture.center.y,current.x-gesture.center.x);
|
|
194
|
+
const delta = angle-gesture.handleAngle;
|
|
195
|
+
gesture.rotation += Math.atan2(Math.sin(delta),Math.cos(delta))*180/Math.PI;
|
|
196
|
+
gesture.handleAngle = angle; value.rotate += gesture.rotation;
|
|
197
|
+
} else if (gesture.mode === 'zoom') value.zoom = gesture.base.zoom * Math.exp(dy/100);
|
|
198
|
+
else if (gesture.mode === 'rotate') value.rotate -= dy;
|
|
199
|
+
else { value.x += dx; value.y += dy; }
|
|
200
|
+
value.zoom = this.#boundedZoom(value.zoom);
|
|
201
|
+
this.#session.setTransform(value); this.#paint();
|
|
202
|
+
}
|
|
203
|
+
#up(event) {
|
|
204
|
+
if (!this.#points.has(event.pointerId)) return;
|
|
205
|
+
this.#move(event); this.#points.delete(event.pointerId);
|
|
206
|
+
if (this.#frame.hasPointerCapture(event.pointerId)) this.#frame.releasePointerCapture(event.pointerId);
|
|
207
|
+
if (this.#points.size) this.#rebase(); else this.#cancelGesture();
|
|
208
|
+
}
|
|
209
|
+
#cancelGesture() {
|
|
210
|
+
const ids = [...this.#points.keys()]; this.#points.clear(); this.#gesture = null; this.#trackpadGesture = null;
|
|
211
|
+
for (const id of ids) if (this.#frame.hasPointerCapture(id)) this.#frame.releasePointerCapture(id);
|
|
212
|
+
if (this.#frame) this.#frame.style.cursor = 'grab';
|
|
213
|
+
}
|
|
214
|
+
#boundedZoom(zoom) {
|
|
215
|
+
return Math.max(this.#minZoom,Math.min(this.#maxZoom,zoom));
|
|
216
|
+
}
|
|
217
|
+
#wheel(event) {
|
|
218
|
+
if (this.#settled || this.#points.size || (!event.ctrlKey && !event.altKey)) return;
|
|
219
|
+
event.preventDefault();
|
|
220
|
+
if (this.#trackpadGesture) return;
|
|
221
|
+
const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? this.#options.height : 1;
|
|
222
|
+
const value = this.#session.transform;
|
|
223
|
+
if (event.ctrlKey) value.zoom = this.#boundedZoom(value.zoom * Math.exp(-event.deltaY * unit / 100));
|
|
224
|
+
else value.rotate -= event.deltaY * unit / 4;
|
|
225
|
+
this.#session.setTransform(value); this.#paint();
|
|
226
|
+
}
|
|
227
|
+
#trackpadStart(event) {
|
|
228
|
+
if (this.#settled || this.#points.size) return;
|
|
229
|
+
event.preventDefault();
|
|
230
|
+
this.#trackpadGesture = this.#session.transform;
|
|
231
|
+
}
|
|
232
|
+
#trackpadChange(event) {
|
|
233
|
+
if (!this.#trackpadGesture || this.#settled) return;
|
|
234
|
+
event.preventDefault();
|
|
235
|
+
if (!Number.isFinite(event.scale) || event.scale <= 0 || !Number.isFinite(event.rotation)) return;
|
|
236
|
+
const value = {...this.#trackpadGesture};
|
|
237
|
+
value.zoom = this.#boundedZoom(value.zoom * event.scale);
|
|
238
|
+
value.rotate += event.rotation;
|
|
239
|
+
this.#session.setTransform(value); this.#paint();
|
|
240
|
+
}
|
|
241
|
+
#trackpadEnd(event) {
|
|
242
|
+
if (!this.#trackpadGesture) return;
|
|
243
|
+
event.preventDefault(); this.#trackpadGesture = null;
|
|
244
|
+
}
|
|
245
|
+
#paint() {
|
|
246
|
+
const {x,y,zoom,rotate} = this.#session.transform;
|
|
247
|
+
this.#frameImage.style.transform = `translate(-50%, -50%) translate(${x}px, ${y}px) rotate(${rotate}deg) scale(${zoom})`;
|
|
248
|
+
}
|
|
249
|
+
open() {
|
|
250
|
+
if (this.#resolve || this.#settled) throw new Error('Image crop dialog has already opened');
|
|
251
|
+
return new Promise((resolve, reject) => {
|
|
252
|
+
this.#resolve = resolve;
|
|
253
|
+
this.#document.body.append(this.#dialog);
|
|
254
|
+
try { this.#dialog.showModal(); }
|
|
255
|
+
catch (error) { this.#settled = true; this.#events.abort(); this.#dialog.remove(); reject(error); }
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
cancel() {
|
|
259
|
+
if (this.#settled) return;
|
|
260
|
+
this.#cancelGesture(); this.#session.cancel();
|
|
261
|
+
if (!this.#resolve) { this.#settled = true; this.#events.abort(); return; }
|
|
262
|
+
this.#finish(null);
|
|
263
|
+
}
|
|
264
|
+
#finish(value) {
|
|
265
|
+
if (this.#settled) return;
|
|
266
|
+
this.#settled = true;
|
|
267
|
+
this.#cancelGesture(); this.#events.abort();
|
|
268
|
+
if (this.#dialog.open) this.#dialog.close();
|
|
269
|
+
this.#dialog.remove();
|
|
270
|
+
this.#resolve(value);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** Rasterize an image transform into a new Blob without modifying the source.
|
|
2
|
+
* x/y are viewport-pixel translations; zoom and rotate follow transformPane.
|
|
3
|
+
* Output dimensions must preserve the viewport aspect ratio.
|
|
4
|
+
*/
|
|
5
|
+
export async function renderImageCrop({document, image, sourceWidth, sourceHeight,
|
|
6
|
+
width, height, outputWidth, outputHeight, x, y, zoom, rotate, type, quality}) {
|
|
7
|
+
const dimensions = [sourceWidth, sourceHeight, width, height, outputWidth, outputHeight];
|
|
8
|
+
if (dimensions.some(value => !Number.isSafeInteger(value) || value <= 0)) {
|
|
9
|
+
throw new TypeError('Crop dimensions must be positive safe integers');
|
|
10
|
+
}
|
|
11
|
+
if (BigInt(outputWidth) * BigInt(height) !== BigInt(outputHeight) * BigInt(width)) throw new RangeError('Crop output must preserve the viewport aspect ratio');
|
|
12
|
+
if (![x, y, zoom, rotate].every(Number.isFinite) || zoom <= 0) throw new TypeError('Invalid image transform');
|
|
13
|
+
if (typeof type !== 'string' || !type.startsWith('image/')) throw new TypeError('Crop output requires an image MIME type');
|
|
14
|
+
if (quality !== undefined && (!Number.isFinite(quality) || quality < 0 || quality > 1)) {
|
|
15
|
+
throw new RangeError('Crop quality must be between zero and one');
|
|
16
|
+
}
|
|
17
|
+
const canvas = document.createElement('canvas');
|
|
18
|
+
canvas.width = outputWidth;
|
|
19
|
+
canvas.height = outputHeight;
|
|
20
|
+
if (canvas.width !== outputWidth || canvas.height !== outputHeight) throw new RangeError('Crop output dimensions are unsupported');
|
|
21
|
+
const context = canvas.getContext('2d');
|
|
22
|
+
if (!context) throw new Error('Image crop canvas is unavailable');
|
|
23
|
+
context.scale(outputWidth / width, outputHeight / height);
|
|
24
|
+
context.translate(width / 2 + x, height / 2 + y);
|
|
25
|
+
context.rotate(rotate * Math.PI / 180);
|
|
26
|
+
context.scale(zoom, zoom);
|
|
27
|
+
context.drawImage(image, -sourceWidth / 2, -sourceHeight / 2, sourceWidth, sourceHeight);
|
|
28
|
+
return await new Promise((resolve, reject) => {
|
|
29
|
+
canvas.toBlob(blob => {
|
|
30
|
+
if (!blob) { reject(new Error('Image crop encoding failed')); return; }
|
|
31
|
+
if (blob.type !== type) { reject(new Error(`Unsupported image format: ${type}`)); return; }
|
|
32
|
+
resolve(blob);
|
|
33
|
+
}, type, quality);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One private editing session; the caller owns the original and decoded image. */
|
|
38
|
+
export class ImageCropSession {
|
|
39
|
+
#document; #original; #image; #sourceWidth; #sourceHeight; #width; #height;
|
|
40
|
+
#state; #generation = 0; #closed = false; #rendering = false;
|
|
41
|
+
constructor({state, document, original, image, sourceWidth, sourceHeight, width, height, x = 0, y = 0, zoom = 1, rotate = 0}) {
|
|
42
|
+
const transform = {x, y, zoom, rotate};
|
|
43
|
+
this.#validate(transform);
|
|
44
|
+
if (!original || !image) throw new TypeError('An original and decoded image are required');
|
|
45
|
+
this.#document = document; this.#original = original; this.#image = image;
|
|
46
|
+
this.#sourceWidth = sourceWidth; this.#sourceHeight = sourceHeight;
|
|
47
|
+
this.#width = width; this.#height = height;
|
|
48
|
+
this.#state = state;
|
|
49
|
+
state.setItem('initial', transform); state.setItem('transform', {...transform});
|
|
50
|
+
}
|
|
51
|
+
#validate({x, y, zoom, rotate}) {
|
|
52
|
+
if (![x, y, zoom, rotate].every(Number.isFinite) || zoom <= 0) throw new TypeError('Invalid image transform');
|
|
53
|
+
}
|
|
54
|
+
#open() { if (this.#closed) throw new Error('Image editing session is closed'); }
|
|
55
|
+
get original() { return this.#original; }
|
|
56
|
+
get transform() { return {...this.#state.getItem('transform')}; }
|
|
57
|
+
setTransform(transform) {
|
|
58
|
+
this.#open();
|
|
59
|
+
if (!transform || Object.keys(transform).sort().join(',') !== 'rotate,x,y,zoom') throw new TypeError('Expected x, y, zoom and rotate');
|
|
60
|
+
this.#validate(transform);
|
|
61
|
+
this.#state.setItem('transform', {...transform}); this.#generation++;
|
|
62
|
+
}
|
|
63
|
+
reset() { this.#open(); this.#state.setItem('transform', {...this.#state.getItem('initial')}); this.#generation++; }
|
|
64
|
+
cancel() { this.#closed = true; this.#generation++; }
|
|
65
|
+
async confirm({outputWidth, outputHeight, type, quality}) {
|
|
66
|
+
this.#open();
|
|
67
|
+
if (this.#rendering) throw new Error('Image crop is already being generated');
|
|
68
|
+
const generation = this.#generation;
|
|
69
|
+
this.#rendering = true;
|
|
70
|
+
try {
|
|
71
|
+
const blob = await renderImageCrop({document:this.#document, image:this.#image,
|
|
72
|
+
sourceWidth:this.#sourceWidth, sourceHeight:this.#sourceHeight, width:this.#width, height:this.#height,
|
|
73
|
+
outputWidth, outputHeight, ...this.#state.getItem('transform'), type, quality});
|
|
74
|
+
if (this.#closed || this.#generation !== generation) throw new DOMException('Image edit cancelled or changed', 'AbortError');
|
|
75
|
+
this.#closed = true;
|
|
76
|
+
return blob;
|
|
77
|
+
} finally { this.#rendering = false; }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/** Editable image value backed by the shared upload engine when an operation is supplied. */
|
|
2
|
+
import {Bag} from '@gramlot/gramlot';
|
|
3
|
+
import {defineComponent} from './registry.js';
|
|
4
|
+
import {controlElement} from './bases.js';
|
|
5
|
+
import {UploadEngine, readUploadContext} from './upload-engine.js';
|
|
6
|
+
import {ImageCropDialog} from './image-crop-dialog.js';
|
|
7
|
+
import {randomId} from '../random-id.js';
|
|
8
|
+
import {captureScreen} from './screen-capture.js';
|
|
9
|
+
|
|
10
|
+
export const ImageUploader = defineComponent({
|
|
11
|
+
name:'imageUploader', tag:'gramlot-image-uploader', base:controlElement, sourceContext:true, bagStore:true,
|
|
12
|
+
parameters:{value:{dtype:'T', nullable:true, default:null, binding:'twoWay'},
|
|
13
|
+
storepath:{dtype:'T'}, contextPath:{dtype:'T',nullable:true,default:null}, operation:{dtype:'T', nullable:true, default:null},
|
|
14
|
+
cropWidth:{dtype:'L', default:256}, cropHeight:{dtype:'L', default:256},
|
|
15
|
+
outputWidth:{dtype:'L', default:512}, outputHeight:{dtype:'L', default:512},
|
|
16
|
+
disabled:{dtype:'B', default:false, binding:'read'}, onComplete:{dtype:'T', nullable:true, default:null}},
|
|
17
|
+
implementation: Base => class extends Base {
|
|
18
|
+
#context; #store; #engine; #original; #dialog; #pending; #generation = 0; #projected; #written;
|
|
19
|
+
#cameraSubscription; #cameraToken = null; #cameraPending = false; #cameraAvailable = false; #cameraStatus; #screenRequest = null;
|
|
20
|
+
constructor() {
|
|
21
|
+
super();
|
|
22
|
+
const doc = this.ownerDocument;
|
|
23
|
+
this.control = doc.createElement('div'); this.controlBox.append(this.control);
|
|
24
|
+
const style = doc.createElement('style');
|
|
25
|
+
style.textContent = ':host{display:block} .image-uploader{display:grid;gap:.5em;justify-items:start} img{max-width:100%;max-height:240px} .actions{display:flex;gap:.5em;flex-wrap:wrap} button{font:inherit} .image-uploader[data-drag-over]{outline:2px dashed var(--gramlot-action,#3766b5);outline-offset:4px}';
|
|
26
|
+
const box = doc.createElement('div'); box.className = 'image-uploader';
|
|
27
|
+
this.preview = doc.createElement('img'); this.preview.alt = 'Selected image';
|
|
28
|
+
this.input = doc.createElement('input'); this.input.type = 'file'; this.input.accept = 'image/png,image/jpeg,image/webp'; this.input.hidden = true;
|
|
29
|
+
const actions = doc.createElement('div'); actions.className = 'actions';
|
|
30
|
+
const button = caption => { const el = doc.createElement('button'); el.type = 'button'; el.textContent = caption; actions.append(el); return el; };
|
|
31
|
+
this.chooseButton = button('Choose image'); this.cameraButton = button('Take photo'); this.screenButton = button('Capture screen'); this.editButton = button('Edit'); this.clearButton = button('Clear');
|
|
32
|
+
this.cancelScreenButton = button('Cancel capture'); this.cancelButton = button('Cancel upload'); this.checkButton = button('Check status');
|
|
33
|
+
this.message = doc.createElement('div'); this.message.setAttribute('role','status');
|
|
34
|
+
box.append(this.preview,this.input,actions,this.message); this.control.append(box); this.shadowRoot.append(style);
|
|
35
|
+
this.chooseButton.addEventListener('click', () => this.input.click());
|
|
36
|
+
box.addEventListener('dragover', event => {
|
|
37
|
+
if (!this.parameters?.disabled && !this.#pending && this.#cameraToken === null && !this.#screenRequest && [...event.dataTransfer.types].includes('Files')) {
|
|
38
|
+
event.preventDefault(); event.stopPropagation(); event.dataTransfer.dropEffect = 'copy'; box.setAttribute('data-drag-over','');
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
box.addEventListener('dragleave', () => box.removeAttribute('data-drag-over'));
|
|
42
|
+
box.addEventListener('drop', event => {
|
|
43
|
+
box.removeAttribute('data-drag-over');
|
|
44
|
+
if (this.parameters?.disabled || this.#pending || this.#cameraToken !== null || this.#screenRequest || ![...event.dataTransfer.types].includes('Files')) return;
|
|
45
|
+
event.preventDefault(); event.stopPropagation();
|
|
46
|
+
const files = [...event.dataTransfer.files];
|
|
47
|
+
if (files.length !== 1) { this.message.textContent = 'Select one image'; return; }
|
|
48
|
+
void this.#choose(files[0]);
|
|
49
|
+
});
|
|
50
|
+
this.cameraButton.addEventListener('click', () => this.#capture());
|
|
51
|
+
this.screenButton.addEventListener('click', () => this.#captureScreen());
|
|
52
|
+
this.cancelScreenButton.addEventListener('click', () => this.#screenRequest?.abort());
|
|
53
|
+
this.input.addEventListener('change', event => { event.stopPropagation(); const file = this.input.files[0]; this.input.value = ''; if (file) void this.#choose(file); });
|
|
54
|
+
this.editButton.addEventListener('click', () => void this.#edit());
|
|
55
|
+
this.clearButton.addEventListener('click', () => { this.#invalidate(); this.#write(null); });
|
|
56
|
+
this.cancelButton.addEventListener('click', () => { if (this.#pending) this.#engine.cancel(this.#pending.id); });
|
|
57
|
+
this.checkButton.addEventListener('click', () => void this.#check());
|
|
58
|
+
}
|
|
59
|
+
setSourceContext(context) {
|
|
60
|
+
this.#context = context;
|
|
61
|
+
if (!context.sourceNode.imageCropState) Object.defineProperty(context.sourceNode,
|
|
62
|
+
'imageCropState', {value:new Bag(), configurable:true});
|
|
63
|
+
}
|
|
64
|
+
readControlValue() { return this.parameters?.value ?? null; }
|
|
65
|
+
writeControlValue(value) { if (this.parameters) { this.parameters.value = value; this.#render(); } }
|
|
66
|
+
setStoreBag(store) {
|
|
67
|
+
if (!(store instanceof Bag)) throw new TypeError('imageUploader storepath must resolve to a Bag');
|
|
68
|
+
if (this.#store && this.#store !== store) { this.#invalidate(); this.#engine?.dispose(); this.#engine = null; }
|
|
69
|
+
this.#store = store;
|
|
70
|
+
}
|
|
71
|
+
projectAttributes(attrs, adapt) {
|
|
72
|
+
const rest = super.projectAttributes(attrs, adapt);
|
|
73
|
+
const p = this.parameters;
|
|
74
|
+
for (const key of ['cropWidth','cropHeight','outputWidth','outputHeight']) {
|
|
75
|
+
if (!Number.isSafeInteger(p[key]) || p[key] <= 0) throw new RangeError(`${key} must be a positive integer`);
|
|
76
|
+
}
|
|
77
|
+
if (p.cropWidth * p.outputHeight !== p.cropHeight * p.outputWidth) throw new RangeError('Crop and output aspect ratios must match');
|
|
78
|
+
if (this.#projected !== undefined && p.value !== this.#projected && p.value !== this.#written) this.#invalidate();
|
|
79
|
+
this.#projected = p.value; this.#written = undefined;
|
|
80
|
+
this.#ensureEngine(); if (this.#engine) this.#engine.operation = p.operation;
|
|
81
|
+
this.#render(); return rest;
|
|
82
|
+
}
|
|
83
|
+
connectedCallback() { this.#ensureEngine(); this.#observeCamera(); }
|
|
84
|
+
disconnectedCallback() {
|
|
85
|
+
// DOM relocation into a floating keeps the same Source owner and resources.
|
|
86
|
+
this.ownerDocument.defaultView.queueMicrotask(() => {
|
|
87
|
+
if (this.isConnected) return;
|
|
88
|
+
if (this.#cameraSubscription) {
|
|
89
|
+
this.#context.upload().gramlot.data.unsubscribe(this.#cameraSubscription,{any:true}); this.#cameraSubscription = null;
|
|
90
|
+
}
|
|
91
|
+
this.#invalidate(); this.#engine?.dispose(); this.#engine = null;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
#observeCamera() {
|
|
95
|
+
if (this.#cameraSubscription || !this.#context) return;
|
|
96
|
+
const {gramlot} = this.#context.upload();
|
|
97
|
+
this.#cameraSubscription = `image-uploader-camera-${randomId()}`;
|
|
98
|
+
gramlot.data.subscribe(this.#cameraSubscription,{any:event => this.#syncCamera(event)});
|
|
99
|
+
this.#syncCamera();
|
|
100
|
+
}
|
|
101
|
+
#syncCamera(event = null) {
|
|
102
|
+
const {gramlot} = this.#context.upload();
|
|
103
|
+
const available = Boolean(gramlot.dvc.provider && gramlot.dvc.provider.capabilities.includes('camera'));
|
|
104
|
+
const camera = gramlot.data.getNode('gramlot.device.camera');
|
|
105
|
+
const status = camera?.attr.status;
|
|
106
|
+
if (available !== this.#cameraAvailable || status !== this.#cameraStatus) {
|
|
107
|
+
this.#cameraAvailable = available; this.#cameraStatus = status; this.#render();
|
|
108
|
+
}
|
|
109
|
+
if (this.#cameraToken === null || event?.node !== camera) return;
|
|
110
|
+
if (status === 'pending') { this.#cameraPending = true; return; }
|
|
111
|
+
if (!this.#cameraPending || !['success','cancelled','denied','unavailable','error'].includes(status)) return;
|
|
112
|
+
const token = this.#cameraToken; this.#cameraToken = null; this.#cameraPending = false; this.#render();
|
|
113
|
+
if (status === 'success') void this.#usePhoto(camera.staticValue.getItem('url'),token);
|
|
114
|
+
else if (status !== 'cancelled') this.message.textContent = camera.attr.error?.message || 'Camera is unavailable';
|
|
115
|
+
}
|
|
116
|
+
#capture() {
|
|
117
|
+
if (!this.#cameraAvailable || this.#cameraStatus === 'pending' || this.parameters.disabled || this.#pending || this.#cameraToken !== null || this.#screenRequest) return;
|
|
118
|
+
this.#cameraToken = ++this.#generation; this.#cameraPending = false;
|
|
119
|
+
try { this.#context.sourceNode.FIRE('gramlot.device.camera?capture'); }
|
|
120
|
+
catch (error) { this.#cameraToken = null; this.message.textContent = `Camera failed: ${error.message}`; this.#render(); }
|
|
121
|
+
}
|
|
122
|
+
#captureScreen() {
|
|
123
|
+
if (this.parameters.disabled || this.#pending || this.#cameraToken !== null || this.#screenRequest || this.#dialog) return;
|
|
124
|
+
const win=this.ownerDocument.defaultView, controller=new win.AbortController(), token=++this.#generation;
|
|
125
|
+
this.#screenRequest=controller; this.message.textContent=''; this.#render();
|
|
126
|
+
const request=captureScreen({document:this.ownerDocument,signal:controller.signal});
|
|
127
|
+
void request.then(blob=>{
|
|
128
|
+
if (token !== this.#generation) return;
|
|
129
|
+
this.#screenRequest=null; this.#render();
|
|
130
|
+
return this.#choose(new win.File([blob],'screen.png',{type:'image/png'}));
|
|
131
|
+
}).catch(error=>{
|
|
132
|
+
if (token !== this.#generation) return;
|
|
133
|
+
this.#screenRequest=null; this.#render();
|
|
134
|
+
if (error.name !== 'AbortError') this.message.textContent = error.name === 'NotAllowedError' ?
|
|
135
|
+
'Screen capture was cancelled or not permitted' : `Screen capture failed: ${error.message}`;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async #usePhoto(url,token) {
|
|
139
|
+
try {
|
|
140
|
+
const candidate = await this.#load(url);
|
|
141
|
+
if (token !== this.#generation) { this.#release(candidate); return; }
|
|
142
|
+
await this.#crop(candidate,token);
|
|
143
|
+
} catch (error) { if (token === this.#generation) this.message.textContent = `Image failed: ${error.message}`; }
|
|
144
|
+
}
|
|
145
|
+
#ensureEngine() {
|
|
146
|
+
if (!this.#engine && this.#context && this.#store && this.parameters) {
|
|
147
|
+
const {gramlot,window} = this.#context.upload();
|
|
148
|
+
this.#engine = new UploadEngine({gramlot,window,store:this.#store,operation:this.parameters.operation,
|
|
149
|
+
onChange:() => this.#render(), getContext:() => readUploadContext(this.#context.sourceNode,this.parameters.contextPath), onComplete:(item,result) => this.#complete(item,result)});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
#release(candidate) { if (candidate?.objectUrl) this.ownerDocument.defaultView.URL.revokeObjectURL(candidate.objectUrl); }
|
|
153
|
+
#invalidate() {
|
|
154
|
+
this.#generation++; this.#screenRequest?.abort(); this.#screenRequest=null; this.#cameraToken = null; this.#cameraPending = false; this.#dialog?.cancel(); this.#dialog = null;
|
|
155
|
+
if (this.#pending) { this.#engine?.cancel(this.#pending.id); if (this.#pending.candidate !== this.#original) this.#release(this.#pending.candidate); this.#pending = null; }
|
|
156
|
+
this.#release(this.#original); this.#original = null;
|
|
157
|
+
}
|
|
158
|
+
#write(value) { this.#written = value; this.#context.writeParameter('value', value); }
|
|
159
|
+
#render() {
|
|
160
|
+
if (!this.parameters) return;
|
|
161
|
+
const value = this.parameters.value;
|
|
162
|
+
if (value) { this.preview.src = value; this.preview.hidden = false; } else { this.preview.removeAttribute('src'); this.preview.hidden = true; }
|
|
163
|
+
const state = this.#pending && this.#store.getItem(this.#pending.id)?.getItem('status');
|
|
164
|
+
const busy = Boolean(this.#pending || this.#cameraToken !== null || this.#screenRequest);
|
|
165
|
+
this.screenButton.hidden = !this.ownerDocument.defaultView.navigator.mediaDevices?.getDisplayMedia;
|
|
166
|
+
this.screenButton.disabled = Boolean(this.parameters.disabled || busy);
|
|
167
|
+
this.cancelScreenButton.hidden = !this.#screenRequest;
|
|
168
|
+
this.cameraButton.hidden = !this.#cameraAvailable;
|
|
169
|
+
this.cameraButton.disabled = Boolean(this.parameters.disabled || busy || this.#cameraStatus === 'pending');
|
|
170
|
+
this.chooseButton.disabled = this.editButton.disabled = this.clearButton.disabled = Boolean(this.parameters.disabled || busy);
|
|
171
|
+
this.editButton.disabled ||= !value && !this.#original;
|
|
172
|
+
this.clearButton.disabled ||= !value;
|
|
173
|
+
this.cancelButton.hidden = !['preparing','uploading'].includes(state);
|
|
174
|
+
this.checkButton.hidden = state !== 'uncertain';
|
|
175
|
+
if (state) this.message.textContent = `Upload: ${state}${this.#store.getItem(this.#pending.id).getItem('error') ? ' — ' + this.#store.getItem(this.#pending.id).getItem('error') : ''}`;
|
|
176
|
+
}
|
|
177
|
+
async #load(source, objectUrl = null) {
|
|
178
|
+
const win = this.ownerDocument.defaultView, image = new win.Image();
|
|
179
|
+
if (!objectUrl) image.crossOrigin = 'anonymous';
|
|
180
|
+
image.src = objectUrl || source;
|
|
181
|
+
try { await image.decode(); }
|
|
182
|
+
catch (error) { if (objectUrl) win.URL.revokeObjectURL(objectUrl); throw error; }
|
|
183
|
+
if (!image.naturalWidth || !image.naturalHeight) { if (objectUrl) win.URL.revokeObjectURL(objectUrl); throw new Error('Image has no dimensions'); }
|
|
184
|
+
const {cropWidth,cropHeight} = this.parameters;
|
|
185
|
+
return {image, original:source, objectUrl, transform:{x:0,y:0,zoom:Math.max(cropWidth/image.naturalWidth,cropHeight/image.naturalHeight),rotate:0}};
|
|
186
|
+
}
|
|
187
|
+
async #choose(file) {
|
|
188
|
+
if (this.parameters.disabled || this.#pending || this.#screenRequest) return;
|
|
189
|
+
if (!['image/png','image/jpeg','image/webp'].includes(file.type)) { this.message.textContent = 'Choose a PNG, JPEG or WebP image'; return; }
|
|
190
|
+
const token = ++this.#generation;
|
|
191
|
+
try {
|
|
192
|
+
const candidate = await this.#load(file, this.ownerDocument.defaultView.URL.createObjectURL(file));
|
|
193
|
+
if (token !== this.#generation) { this.#release(candidate); return; }
|
|
194
|
+
await this.#crop(candidate,token);
|
|
195
|
+
} catch (error) { if (token === this.#generation) this.message.textContent = `Image failed: ${error.message}`; }
|
|
196
|
+
}
|
|
197
|
+
async #edit() {
|
|
198
|
+
if (this.parameters.disabled || this.#pending || this.#screenRequest) return;
|
|
199
|
+
const token = ++this.#generation;
|
|
200
|
+
try {
|
|
201
|
+
const candidate = this.#original || await this.#load(this.parameters.value);
|
|
202
|
+
if (token !== this.#generation) { if (candidate !== this.#original) this.#release(candidate); return; }
|
|
203
|
+
await this.#crop(candidate,token);
|
|
204
|
+
} catch (error) { if (token === this.#generation) this.message.textContent = `Image failed: ${error.message}`; }
|
|
205
|
+
}
|
|
206
|
+
async #crop(candidate,token) {
|
|
207
|
+
const p = this.parameters;
|
|
208
|
+
const dialog = this.#dialog = new ImageCropDialog({state:this.#context.sourceNode.imageCropState,document:this.ownerDocument, original:candidate.original, image:candidate.image,
|
|
209
|
+
sourceWidth:candidate.image.naturalWidth, sourceHeight:candidate.image.naturalHeight,
|
|
210
|
+
width:p.cropWidth,height:p.cropHeight,outputWidth:p.outputWidth,outputHeight:p.outputHeight,type:'image/png',...candidate.transform});
|
|
211
|
+
let result;
|
|
212
|
+
try { result = await dialog.open(); } catch (error) { if (candidate !== this.#original) this.#release(candidate); throw error; }
|
|
213
|
+
if (this.#dialog === dialog) this.#dialog = null;
|
|
214
|
+
if (!result || token !== this.#generation) { if (candidate !== this.#original) this.#release(candidate); return; }
|
|
215
|
+
if (p.operation === null) {
|
|
216
|
+
try {
|
|
217
|
+
const data = await new Promise((resolve,reject) => { const reader = new this.ownerDocument.defaultView.FileReader();
|
|
218
|
+
reader.onload = () => resolve(reader.result); reader.onerror = () => reject(reader.error); reader.readAsDataURL(result.blob); });
|
|
219
|
+
if (token !== this.#generation) { if (candidate !== this.#original) this.#release(candidate); return; }
|
|
220
|
+
this.#commit(candidate,result.transform,data); this.message.textContent = 'Image updated';
|
|
221
|
+
} catch (error) { if (candidate !== this.#original) this.#release(candidate); throw error; }
|
|
222
|
+
} else {
|
|
223
|
+
const name = typeof candidate.original === 'string' ? 'image.png' : candidate.original.name.replace(/\.[^.]*$/, '') + '.png';
|
|
224
|
+
const file = new this.ownerDocument.defaultView.File([result.blob], name, {type:'image/png'});
|
|
225
|
+
const id = this.#engine.add(file); this.#pending = {id,candidate,transform:result.transform,token};
|
|
226
|
+
await this.#engine.start(id); this.#settle(id);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
#commit(candidate,transform,value) {
|
|
230
|
+
if (this.#original !== candidate) this.#release(this.#original);
|
|
231
|
+
candidate.transform = transform; this.#original = candidate; this.#write(value);
|
|
232
|
+
}
|
|
233
|
+
async #complete(item,result) {
|
|
234
|
+
const pending = this.#pending;
|
|
235
|
+
if (!pending || pending.id !== item.label || pending.token !== this.#generation) return;
|
|
236
|
+
if (typeof result !== 'string' || !result) throw new TypeError('Image upload must return a displayable URL');
|
|
237
|
+
this.#commit(pending.candidate,pending.transform,result);
|
|
238
|
+
this.#pending = null; this.#render();
|
|
239
|
+
if (this.parameters.onComplete) await this.#context.callLogic(this.parameters.onComplete,{item,result});
|
|
240
|
+
}
|
|
241
|
+
#settle(id) {
|
|
242
|
+
if (!this.#pending || this.#pending.id !== id) return;
|
|
243
|
+
const row = this.#store.getItem(id), state = row.getItem('status');
|
|
244
|
+
if (['failed','cancelled','completed'].includes(state)) {
|
|
245
|
+
const error = row.getItem('error');
|
|
246
|
+
if (this.#pending.candidate !== this.#original) this.#release(this.#pending.candidate);
|
|
247
|
+
this.#pending = null; this.#render(); this.message.textContent = error || `Upload: ${state}`;
|
|
248
|
+
} else this.#render();
|
|
249
|
+
}
|
|
250
|
+
async #check() { if (!this.#pending) return; const id = this.#pending.id; await this.#engine.reconcile(id); this.#settle(id); }
|
|
251
|
+
}
|
|
252
|
+
});
|
package/src/components/inputs.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {EditingStatus} from './editing-status.js';
|
|
1
2
|
import {defineComponent, defineCollection} from './registry.js';
|
|
2
3
|
import {NativeControl, ChoiceGroup} from './bases.js';
|
|
3
4
|
import {Labelbox} from './labelbox.js';
|
|
@@ -197,7 +198,7 @@ export const ComboBox = defineComponent({
|
|
|
197
198
|
parameters: {values: {dtype: 'T', default: '', binding: 'read'}, separator: {dtype: 'T', default: ',', binding: 'read'},
|
|
198
199
|
store: {dtype: 'X', nullable: true, default: null, binding: 'read'}},
|
|
199
200
|
implementation: Base => class extends localChoices(Base) {
|
|
200
|
-
get constrained() { return false; }
|
|
201
|
+
static get constrained() { return false; }
|
|
201
202
|
}
|
|
202
203
|
});
|
|
203
204
|
export const DbSelect = defineComponent({
|
|
@@ -208,7 +209,7 @@ export const DbSelect = defineComponent({
|
|
|
208
209
|
export const DbComboBox = defineComponent({
|
|
209
210
|
name:'dbComboBox', tag:'gramlot-db-combo-box', base:ChoiceEditor, mixins:[Labelbox, FieldState],
|
|
210
211
|
parameters:databaseParameters, implementation:Base => class extends databaseChoices(Base) {
|
|
211
|
-
get constrained() { return false; }
|
|
212
|
+
static get constrained() { return false; }
|
|
212
213
|
}
|
|
213
214
|
});
|
|
214
215
|
export const CheckBoxText = defineComponent({
|
|
@@ -223,6 +224,6 @@ export const CheckBoxText = defineComponent({
|
|
|
223
224
|
get multiple() { return true; }
|
|
224
225
|
}
|
|
225
226
|
});
|
|
226
|
-
export default defineCollection({name: 'inputs', components: [Textbox, Checkbox, RadioButtonText,
|
|
227
|
+
export default defineCollection({name: 'inputs', components: [EditingStatus, Textbox, Checkbox, RadioButtonText,
|
|
227
228
|
TextBoxArea, Passwordbox, RadioButton, Colorpicker, HorizontalSlider, VerticalSlider,
|
|
228
229
|
NumberTextBox, DateTextBox, TimeTextBox, FilteringSelect, CallbackSelect, ComboBox, DbSelect, DbComboBox, CheckBoxText, DateCalendar]});
|