aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/global/globalScripts.ts
blob: c595681b7138355927aba2e6d433c27fb0ce2c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import { Colors } from 'browndash-components';
import { action, runInAction } from 'mobx';
import { aggregateBounds } from '../../../Utils';
import { ActiveFillColor, ActiveInkColor, ActiveInkHideTextLabels, ActiveInkWidth, ActiveIsInkMask, Doc, Opt, SetActiveFillColor, SetActiveInkColor, SetActiveInkHideTextLabels, SetActiveInkWidth, SetActiveIsInkMask } from '../../../fields/Doc';
import { DocData } from '../../../fields/DocSymbols';
import { InkTool } from '../../../fields/InkField';
import { BoolCast, Cast, NumCast, StrCast } from '../../../fields/Types';
import { WebField } from '../../../fields/URLField';
import { Gestures } from '../../../pen-gestures/GestureTypes';
import { DocumentType } from '../../documents/DocumentTypes';
import { LinkManager } from '../../util/LinkManager';
import { ScriptingGlobals } from '../../util/ScriptingGlobals';
import { UndoManager, undoable } from '../../util/UndoManager';
import { GestureOverlay } from '../GestureOverlay';
import { InkingStroke } from '../InkingStroke';
import { CollectionFreeFormView } from '../collections/collectionFreeForm';
import { CollectionFreeFormDocumentView } from '../nodes/CollectionFreeFormDocumentView';
import { DocumentView } from '../nodes/DocumentView';
import { ImageBox } from '../nodes/ImageBox';
import { VideoBox } from '../nodes/VideoBox';
import { WebBox } from '../nodes/WebBox';
import { RichTextMenu } from '../nodes/formattedText/RichTextMenu';

// import { InkTranscription } from '../InkTranscription';

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function IsNoneSelected() {
    return DocumentView.Selected().length <= 0;
}, 'are no document selected');

// toggle: Set overlay status of selected document
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function setView(view: string, getSelected: boolean) {
    if (getSelected) return DocumentView.SelectedDocs();
    const selected = DocumentView.SelectedDocs().lastElement();
    selected ? (selected._type_collection = view) : console.log('[FontIconBox.tsx] changeView failed');
    return undefined;
});

// toggle: Set overlay status of selected document
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function setBackgroundColor(color?: string, checkResult?: boolean) {
    const selectedViews = DocumentView.Selected();
    if (Doc.ActiveTool !== InkTool.None) {
        if (checkResult) {
            return ActiveFillColor();
        }
        SetActiveFillColor(color ?? 'transparent');
    } else if (selectedViews.length) {
        if (checkResult) {
            const selView = selectedViews.lastElement();
            const fieldKey = selView.Document._layout_isSvg ? 'fillColor' : 'backgroundColor';
            const layoutFrameNumber = Cast(selView.containerViewPath?.().lastElement()?.Document?._currentFrame, 'number'); // frame number that container is at which determines layout frame values
            const contentFrameNumber = Cast(selView.Document?._currentFrame, 'number', layoutFrameNumber ?? null); // frame number that content is at which determines what content is displayed
            return CollectionFreeFormDocumentView.getStringValues(selView?.Document, contentFrameNumber)[fieldKey] ?? 'transparent';
        }
        selectedViews.some(dv => dv.ComponentView instanceof InkingStroke) && SetActiveFillColor(color ?? 'transparent');
        selectedViews.forEach(dv => {
            const fieldKey = dv.Document._layout_isSvg ? 'fillColor' : 'backgroundColor';
            const layoutFrameNumber = Cast(dv.containerViewPath?.().lastElement()?.Document?._currentFrame, 'number'); // frame number that container is at which determines layout frame values
            const contentFrameNumber = Cast(dv.Document?._currentFrame, 'number', layoutFrameNumber ?? null); // frame number that content is at which determines what content is displayed
            if (contentFrameNumber !== undefined) {
                const obj: { [key: string]: Opt<string> } = {};
                obj[fieldKey] = color;
                CollectionFreeFormDocumentView.setStringValues(contentFrameNumber, dv.Document, obj);
            } else {
                const dataKey = Doc.LayoutFieldKey(dv.Document);
                const alternate = (dv.layoutDoc[dataKey + '_usePath'] ? '_' + dv.layoutDoc[dataKey + '_usePath'] : '').replace(':hover', '');
                dv.dataDoc[fieldKey + alternate] = color;
            }
        });
    } else {
        const selected = DocumentView.SelectedDocs().length ? DocumentView.SelectedDocs() : LinkManager.Instance.currentLink ? [LinkManager.Instance.currentLink] : [];
        if (checkResult) {
            return selected.lastElement()?._backgroundColor ?? 'transparent';
        }
        SetActiveFillColor(color ?? 'transparent');
        selected.forEach(doc => { doc[DocData].backgroundColor = color; }); // prettier-ignore
    }
    return '';
});

// toggle: Set overlay status of selected document
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function setDefaultTemplate(checkResult?: boolean) {
    return DocumentView.setDefaultTemplate(checkResult);
});
// toggle: Set overlay status of selected document
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function setHeaderColor(color?: string, checkResult?: boolean) {
    if (checkResult) {
        return DocumentView.Selected().length ? StrCast(DocumentView.SelectedDocs().lastElement().layout_headingColor) : Doc.SharingDoc().headingColor;
    }
    if (DocumentView.Selected().length) {
        DocumentView.SelectedDocs().forEach(doc => {
            doc[DocData].layout_headingColor = color === 'transparent' ? undefined : color;
            doc.layout_showTitle = color === 'transparent' ? undefined : StrCast(doc.layout_showTitle, 'title');
        });
    } else {
        Doc.SharingDoc().headingColor = undefined;
        Doc.GetProto(Doc.SharingDoc()).headingColor = color === 'transparent' ? undefined : color;
        Doc.UserDoc().layout_showTitle = color === 'transparent' ? undefined : StrCast(Doc.UserDoc().layout_showTitle, 'title');
    }
    return undefined;
});

// toggle: Set overlay status of selected document
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function toggleOverlay(checkResult?: boolean) {
    const selected = DocumentView.Selected().length ? DocumentView.Selected()[0] : undefined;
    if (checkResult) {
        if (NumCast(selected?.Document.z) >= 1) return true;
        return false;
    }
    selected ? CollectionFreeFormDocumentView.from(selected)?.float() : console.log('[FontIconBox.tsx] toggleOverlay failed');
    return undefined;
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function showFreeform(attr: 'center' | 'grid' | 'snaplines' | 'clusters' | 'viewAll' | 'fitOnce', checkResult?: boolean, persist?: boolean) {
    const selected = DocumentView.SelectedDocs().lastElement();
    // prettier-ignore
    const map: Map<'center' |'grid' | 'snaplines' | 'clusters' | 'viewAll' | 'fitOnce', { waitForRender?: boolean, checkResult: (doc:Doc) => any; setDoc: (doc:Doc, dv:DocumentView) => void;}> = new Map([
        ['grid', {
            checkResult: (doc:Doc) => BoolCast(doc?._freeform_backgroundGrid, false),
            setDoc: (doc:Doc) => { doc._freeform_backgroundGrid = !doc._freeform_backgroundGrid; },
        }],
        ['snaplines', {
            checkResult: (doc:Doc) => BoolCast(doc?._freeform_snapLines, false),
            setDoc: (doc:Doc) => { doc._freeform_snapLines = !doc._freeform_snapLines; },
        }],
        ['viewAll', {
            checkResult: (doc:Doc) => BoolCast(doc?._freeform_fitContentsToBox, false),
            setDoc: (doc:Doc,dv:DocumentView) => {
                if (persist) doc._freeform_fitContentsToBox = !doc._freeform_fitContentsToBox;
                else if (doc._freeform_fitContentsToBox) doc._freeform_fitContentsToBox = undefined;
                else (dv.ComponentView as CollectionFreeFormView)?.fitContentOnce();
            },
        }],
        ['center', {
            checkResult: (doc:Doc) => BoolCast(doc?._stacking_alignCenter, false),
            setDoc: (doc:Doc) => { doc._stacking_alignCenter = !doc._stacking_alignCenter; },
        }],
        ['clusters', {
            waitForRender: true, // flags that undo batch should terminate after a re-render giving the script the chance to fire
            checkResult: (doc:Doc) => BoolCast(doc?._freeform_useClusters, false),
            setDoc: (doc:Doc) => { doc._freeform_useClusters = !doc._freeform_useClusters; },
        }],
   ]);

    if (checkResult) {
        return map.get(attr)?.checkResult(selected);
    }
    const batch = map.get(attr)?.waitForRender ? UndoManager.StartBatch('set freeform attribute') : { end: () => {} };
    DocumentView.Selected().map(dv => map.get(attr)?.setDoc(dv.layoutDoc, dv));
    setTimeout(() => batch.end(), 100);
    return undefined;
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function setFontAttr(attr: 'font' | 'fontColor' | 'highlight' | 'fontSize' | 'alignment', value: any, checkResult?: boolean) {
    const editorView = RichTextMenu.Instance?.TextView?.EditorView;
    // prettier-ignore
    const map: Map<'font'|'fontColor'|'highlight'|'fontSize'|'alignment', { checkResult: () => any; setDoc: () => void;}> = new Map([
        ['font', {
            checkResult: () => RichTextMenu.Instance?.fontFamily,
            setDoc: () => value && RichTextMenu.Instance?.setFontField(value, 'fontFamily'),
        }],
        ['highlight', {
            checkResult: () => RichTextMenu.Instance?.fontHighlight,
            setDoc: () => value  && RichTextMenu.Instance?.setFontField(value, 'fontHighlight'),
        }],
        ['fontColor', {
            checkResult: () => RichTextMenu.Instance?.fontColor,
            setDoc: () => value && RichTextMenu.Instance?.setFontField(value, 'fontColor'),
        }],
        ['alignment', {
            checkResult: () => RichTextMenu.Instance?.textAlign,
            setDoc: () => { value && editorView?.state ? RichTextMenu.Instance?.align(editorView, editorView.dispatch, value):(Doc.UserDoc().textAlign = value); },
        }],
        ['fontSize', {
            checkResult: () => RichTextMenu.Instance?.fontSize.replace('px', ''),
            setDoc: () => {
                let fsize = value;
                if (typeof fsize === 'number') fsize = fsize.toString();
                if (fsize && Number(fsize).toString() === fsize) fsize += 'px';
                RichTextMenu.Instance?.setFontField(fsize, 'fontSize');
            },
        }],
    ]);

    if (checkResult) {
        return map.get(attr)?.checkResult();
    }
    map.get(attr)?.setDoc?.();
    return undefined;
});

type attrname = 'noAutoLink' | 'dictation' | 'bold' | 'italics' | 'elide' | 'underline' | 'left' | 'center' | 'right' | 'vcent' | 'bullet' | 'decimal';
type attrfuncs = [attrname, { checkResult: () => boolean; toggle?: () => any }];

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function toggleCharStyle(charStyle: attrname, checkResult?: boolean) {
    const textView = RichTextMenu.Instance?.TextView;
    const editorView = textView?.EditorView;
    // prettier-ignore
    const alignments:attrfuncs[] = (['left','right','center','vcent'] as ("left"|"center"|"right"|"vcent")[]).map((where) =>
        [ where, {      checkResult: () => editorView ? (where === 'vcent' ?  RichTextMenu.Instance?.textVcenter ?? false:
                                                                             (RichTextMenu.Instance?.textAlign === where)): 
                                                         where === 'vcent' ?  BoolCast(Doc.UserDoc()._layout_centered):
                                                                            (Doc.UserDoc().textAlign === where),
                        toggle: () => { editorView?.state ? (where === 'vcent' ? RichTextMenu.Instance?.vcenterToggle():
                                                                                 RichTextMenu.Instance?.align(editorView, editorView.dispatch, where)):
                                                            where === 'vcent' ?  Doc.UserDoc()._layout_centered = !Doc.UserDoc()._layout_centered:
                                                                                (Doc.UserDoc().textAlign = where); }
        }]); // prettier-ignore
    // prettier-ignore
    const listings:attrfuncs[] = (['bullet','decimal'] as attrname[]).map(list =>
        [ list, {       checkResult: () => (editorView ? RichTextMenu.Instance?.listStyle === list:false),
                        toggle: () => editorView?.state && RichTextMenu.Instance?.changeListType(list) }]);
    // prettier-ignore
    const attrs:attrfuncs[] = [
        ['dictation', { checkResult: () => !!textView?._recordingDictation,
                        toggle: () => textView && runInAction(() => { textView._recordingDictation = !textView._recordingDictation;} ) }],
        ['elide',      { checkResult: () => false,
                        toggle: () => editorView ? RichTextMenu.Instance?.elideSelection(): 0}],
        ['noAutoLink',{ checkResult: () => ((editorView && RichTextMenu.Instance?.noAutoLink) ?? false),
                        toggle: () => editorView && RichTextMenu.Instance?.toggleNoAutoLinkAnchor()}],
        ['bold',      { checkResult: () => (editorView ? RichTextMenu.Instance?.bold??false : (Doc.UserDoc().fontWeight === 'bold')),
                        toggle: editorView ? RichTextMenu.Instance?.toggleBold : () => { Doc.UserDoc().fontWeight = Doc.UserDoc().fontWeight === 'bold' ? undefined : 'bold'; }}],
        ['italics',   { checkResult: () => (editorView ? RichTextMenu.Instance?.italics ?? false : (Doc.UserDoc().fontStyle === 'italics')),
                        toggle: editorView ? RichTextMenu.Instance?.toggleItalics : () => { Doc.UserDoc().fontStyle = Doc.UserDoc().fontStyle === 'italics' ? undefined : 'italics'; }}],
        ['underline', { checkResult: () => (editorView ? RichTextMenu.Instance?.underline ?? false: (Doc.UserDoc().textDecoration === 'underline')),
                        toggle: editorView ? RichTextMenu.Instance?.toggleUnderline : () => { Doc.UserDoc().textDecoration = Doc.UserDoc().textDecoration === 'underline' ? undefined : 'underline'; } }]]

    const map = new Map(attrs.concat(alignments).concat(listings));
    if (checkResult) {
        return map.get(charStyle)?.checkResult();
    }
    undoable(() => map.get(charStyle)?.toggle?.(), 'toggle ' + charStyle)();
    return undefined;
});

export function createInkGroup(/* inksToGroup?: Doc[], isSubGroup?: boolean */) {
    // TODO nda - if document being added to is a inkGrouping then we can just add to that group
    if (Doc.ActiveTool === InkTool.Write) {
        CollectionFreeFormView.collectionsWithUnprocessedInk.forEach(ffView => {
            // TODO: nda - will probably want to go through ffView unprocessed docs and then see if any of the inksToGroup docs are in it and only use those
            const selected = ffView.unprocessedDocs;
            // loop through selected an get the bound
            const bounds: { x: number; y: number; width?: number; height?: number }[] = [];

            selected.map(
                action(d => {
                    const x = NumCast(d.x);
                    const y = NumCast(d.y);
                    const width = NumCast(d._width);
                    const height = NumCast(d._height);
                    bounds.push({ x, y, width, height });
                })
            );

            const aggregBounds = aggregateBounds(bounds, 0, 0);
            const marqViewRef = ffView._marqueeViewRef.current;

            // set the vals for bounds in marqueeView
            if (marqViewRef) {
                marqViewRef._downX = aggregBounds.x;
                marqViewRef._downY = aggregBounds.y;
                marqViewRef._lastX = aggregBounds.r;
                marqViewRef._lastY = aggregBounds.b;
            }

            selected.map(
                action(d => {
                    const dx = NumCast(d.x);
                    const dy = NumCast(d.y);
                    delete d.x;
                    delete d.y;
                    delete d.activeFrame;
                    delete d._timecodeToShow; // bcz: this should be automatic somehow.. along with any other properties that were logically associated with the original collection
                    delete d._timecodeToHide; // bcz: this should be automatic somehow.. along with any other properties that were logically associated with the original collection
                    // calculate pos based on bounds
                    if (marqViewRef?.Bounds) {
                        d.x = dx - marqViewRef.Bounds.left - marqViewRef.Bounds.width / 2;
                        d.y = dy - marqViewRef.Bounds.top - marqViewRef.Bounds.height / 2;
                    }
                    return d;
                })
            );
            ffView._props.removeDocument?.(selected);
            // TODO: nda - this is the code to actually get a new grouped collection
            const newCollection = marqViewRef?.getCollection(selected, undefined, true);
            if (newCollection) {
                newCollection.height = NumCast(newCollection._height);
                newCollection.width = NumCast(newCollection._width);
            }

            // nda - bug: when deleting a stroke before leaving writing mode, delete the stroke from unprocessed ink docs
            newCollection && ffView._props.addDocument?.(newCollection);
            // TODO: nda - will probably need to go through and only remove the unprocessed selected docs
            ffView.unprocessedDocs = [];

            //  InkTranscription.Instance.transcribeInk(newCollection, selected, false);
        });
    }
    CollectionFreeFormView.collectionsWithUnprocessedInk.clear();
}

function setActiveTool(tool: InkTool | Gestures, keepPrim: boolean, checkResult?: boolean) {
    // InkTranscription.Instance?.createInkGroup();
    if (checkResult) {
        return (Doc.ActiveTool === tool && !GestureOverlay.Instance?.InkShape) || GestureOverlay.Instance?.InkShape === tool
            ? GestureOverlay.Instance?.KeepPrimitiveMode || ![Gestures.Circle, Gestures.Line, Gestures.Rectangle].includes(tool as Gestures)
            : false;
    }
    runInAction(() => {
        if (GestureOverlay.Instance) {
            GestureOverlay.Instance.KeepPrimitiveMode = keepPrim;
        }
        if (Object.values(Gestures).includes(tool as any)) {
            if (GestureOverlay.Instance.InkShape === tool && !keepPrim) {
                Doc.ActiveTool = InkTool.None;
                GestureOverlay.Instance.InkShape = undefined;
            } else {
                Doc.ActiveTool = InkTool.Pen;
                GestureOverlay.Instance.InkShape = tool as Gestures;
            }
        } else if (tool) {
            // pen or eraser
            if (Doc.ActiveTool === tool && !GestureOverlay.Instance.InkShape && !keepPrim) {
                Doc.ActiveTool = InkTool.None;
            } else {
                Doc.ActiveTool = tool as any;
                GestureOverlay.Instance.InkShape = undefined;
            }
        } else {
            Doc.ActiveTool = InkTool.None;
        }
    });
    return undefined;
}

ScriptingGlobals.add(setActiveTool, 'sets the active ink tool mode');

// toggle: Set overlay status of selected document
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function setInkProperty(option: 'inkMask' | 'labels' | 'fillColor' | 'strokeWidth' | 'strokeColor', value: any, checkResult?: boolean) {
    const selected = DocumentView.SelectedDocs().lastElement() ?? Doc.UserDoc();
    // prettier-ignore
    const map: Map<'inkMask' | 'labels' | 'fillColor' | 'strokeWidth' | 'strokeColor', { checkResult: () => any; setInk: (doc: Doc) => void; setMode: () => void }> = new Map([
        ['inkMask', {
            checkResult: () => ((selected?._layout_isSvg ? BoolCast(selected[DocData].stroke_isInkMask) : ActiveIsInkMask())),
            setInk: (doc: Doc) => { doc[DocData].stroke_isInkMask = !doc.stroke_isInkMask; },
            setMode: () => selected?.type !== DocumentType.INK && SetActiveIsInkMask(!ActiveIsInkMask()),
        }],
        ['labels', {
            checkResult: () => ((selected?._stroke_showLabel ? BoolCast(selected[DocData].stroke_showLabel) : ActiveInkHideTextLabels())),
            setInk: (doc: Doc) => { doc[DocData].stroke_showLabel = !doc.stroke_showLabel; },
            setMode: () => selected?.type !== DocumentType.INK && SetActiveInkHideTextLabels(!ActiveInkHideTextLabels()),
        }],
        ['fillColor', {
            checkResult: () => (selected?._layout_isSvg ? StrCast(selected[DocData].fillColor) : ActiveFillColor() ?? "transparent"),
            setInk: (doc: Doc) => { doc[DocData].fillColor = StrCast(value); },
            setMode: () => SetActiveFillColor(StrCast(value)),
        }],
        [ 'strokeWidth', {
            checkResult: () => (selected?._layout_isSvg ? NumCast(selected[DocData].stroke_width) : ActiveInkWidth()),
            setInk: (doc: Doc) => { doc[DocData].stroke_width = NumCast(value); },
            setMode: () => { SetActiveInkWidth(value.toString()); selected?.type === DocumentType.INK && setActiveTool( GestureOverlay.Instance.InkShape ?? InkTool.Pen, true, false);},
        }],
        ['strokeColor', {
            checkResult: () => (selected?._layout_isSvg? StrCast(selected[DocData].color) : ActiveInkColor()),
            setInk: (doc: Doc) => { doc[DocData].color = String(value); },
            setMode: () => { SetActiveInkColor(StrCast(value)); selected?.type === DocumentType.INK &&  setActiveTool(GestureOverlay.Instance.InkShape ?? InkTool.Pen, true, false);},
        }],
    ]);

    if (checkResult) {
        return map.get(option)?.checkResult();
    }
    map.get(option)?.setMode();
    DocumentView.SelectedDocs()
        .filter(doc => doc._layout_isSvg)
        .map(doc => map.get(option)?.setInk(doc));
    return undefined;
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function toggleRaiseOnDrag(readOnly?: boolean) {
    if (readOnly) {
        return DocumentView.Selected().some(dv => dv.Document.keepZWhenDragged);
    }
    DocumentView.Selected().forEach(dv => {
        dv.Document.keepZWhenDragged = !dv.Document.keepZWhenDragged;
    });
    return undefined;
});

/** WEB
 *      webSetURL
 * */
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function webSetURL(url: string, checkResult?: boolean) {
    const selected = DocumentView.Selected().lastElement();
    if (selected?.Document.type === DocumentType.WEB) {
        if (checkResult) {
            return StrCast(selected.Document.data, Cast(selected.Document.data, WebField, null)?.url?.href);
        }
        selected.ComponentView?.setData?.(url);
    }
    return '';
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function webForward(checkResult?: boolean) {
    const selected = DocumentView.Selected().lastElement()?.ComponentView as WebBox;
    if (checkResult) {
        return selected?.forward(checkResult) ? undefined : 'lightGray';
    }
    selected?.forward();
    return undefined;
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function webBack() {
    const selected = DocumentView.Selected().lastElement()?.ComponentView as WebBox;
    selected?.back();
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function videoSnapshot() {
    const selected = DocumentView.Selected().lastElement()?.ComponentView as VideoBox;
    selected?.Snapshot();
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function imageSetPixelSize() {
    const selected = DocumentView.Selected().lastElement()?.ComponentView as ImageBox;
    selected?.setNativeSize();
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function imageRotate90() {
    const selected = DocumentView.Selected().lastElement()?.ComponentView as ImageBox;
    selected?.rotate();
});

/** Schema
 *      toggleSchemaPreview
 * */
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function toggleSchemaPreview(checkResult?: boolean) {
    const selected = DocumentView.SelectedDocs().lastElement();
    if (checkResult && selected) {
        const result: boolean = NumCast(selected.schema_previewWidth) > 0;
        if (result) return Colors.MEDIUM_BLUE;
        return 'transparent';
    }
    if (selected) {
        if (NumCast(selected.schema_previewWidth) > 0) {
            selected.schema_previewWidth = 0;
        } else {
            selected.schema_previewWidth = 200;
        }
    }
    return '';
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function toggleSingleLineSchema(checkResult?: boolean) {
    const selected = DocumentView.SelectedDocs().lastElement();
    if (checkResult && selected) {
        return NumCast(selected._schema_singleLine) > 0 ? Colors.MEDIUM_BLUE : 'transparent';
    }
    if (selected) {
        selected._schema_singleLine = !selected._schema_singleLine;
    }
    return undefined;
});

/** STACK
 *      groupBy
 */
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function setGroupBy(key: string, checkResult?: boolean) {
    DocumentView.SelectedDocs().forEach(doc => { doc._text_fontFamily = key; }); // prettier-ignore
    const editorView = RichTextMenu.Instance?.TextView?.EditorView;
    if (checkResult) {
        return StrCast((editorView ? RichTextMenu.Instance : Doc.UserDoc())?.fontFamily);
    }
    if (editorView) RichTextMenu.Instance?.setFontField(key, 'fontFamily');
    else Doc.UserDoc().fontFamily = key;
    return undefined;
});