# @embedpdf/viewer-chrome — ./commands FORM_DESIGN_PANEL [VariableDeclaration]: FORM_DESIGN_PANEL = 'form-design' FORM_DESIGN_TOOL [VariableDeclaration]: FORM_DESIGN_TOOL = 'form-edit' GO_TO_PAGE_DIALOG [VariableDeclaration]: GO_TO_PAGE_DIALOG = 'go-to-page' MODE_SURFACES [VariableDeclaration]: MODE_SURFACES = [ 'mode:annotate', 'mode:shapes', 'mode:insert', 'mode:form', 'mode:redact', 'mode:measure', ] as const PRINT_DIALOG [VariableDeclaration]: PRINT_DIALOG = 'print' REDACT_CONFIRM_DIALOG [VariableDeclaration]: REDACT_CONFIRM_DIALOG = 'redact-confirm' SHORTCUTS_DIALOG [VariableDeclaration]: SHORTCUTS_DIALOG = 'shortcuts' TOOL_ICONS [VariableDeclaration]: TOOL_ICONS: Record ={ } ToolAccentDefinition [InterfaceDeclaration]: export interface ToolAccentDefinition{ primary: ColorKey; secondary?: ColorKey; } defaultCommands [VariableDeclaration]: defaultCommands: CommandDefinition[] = [{ id: 'zoom:in', labelKey: 'commands.zoom.in', icon: 'zoomIn', shortcut: ['Mod+=', 'Mod+NumpadAdd'], run: (commandContext) => stage(commandContext)?.zoomIn(), enabled: (commandContext) => stage(commandContext)?.canZoomIn() ?? false, } , { id: 'zoom:out', labelKey: 'commands.zoom.out', icon: 'zoomOut', shortcut: ['Mod+-', 'Mod+NumpadSubtract'], run: (commandContext) => stage(commandContext)?.zoomOut(), enabled: (commandContext) => stage(commandContext)?.canZoomOut() ?? false, } , { id: 'zoom:fit-page', labelKey: 'commands.zoom.fitPage', icon: 'fitToPage', shortcut: 'Mod+0', run: (commandContext) => stage(commandContext)?.fitPage(), active: (commandContext) => stage(commandContext)?.getZoomMode() === ZoomMode.FitPage, enabled: (commandContext) => stage(commandContext) != null, } , { id: 'zoom:fit-width', labelKey: 'commands.zoom.fitWidth', icon: 'fitToWidth', shortcut: 'Mod+1', run: (commandContext) => stage(commandContext)?.fitWidth(), active: (commandContext) => stage(commandContext)?.getZoomMode() === ZoomMode.FitWidth, enabled: (commandContext) => stage(commandContext) != null, } , { id: 'zoom:automatic', labelKey: 'commands.zoom.automatic', run: (commandContext) => stage(commandContext)?.fitAutomatic(), active: (commandContext) => stage(commandContext)?.getZoomMode() === ZoomMode.Automatic, enabled: (commandContext) => stage(commandContext) != null, } , zoomLevel('zoom:50', 0.5, 'commands.zoom.p50'), zoomLevel('zoom:100', 1, 'commands.zoom.p100'), zoomLevel('zoom:150', 1.5, 'commands.zoom.p150'), zoomLevel('zoom:200', 2, 'commands.zoom.p200'), zoomLevel('zoom:400', 4, 'commands.zoom.p400'), { id: 'menu:zoom', labelKey: 'commands.zoom.menu', icon: 'zoomIn', menu: 'zoom', } , { id: 'page:next', labelKey: 'commands.page.next', icon: 'chevronRight', shortcut: 'ArrowRight', run: (commandContext) => stage(commandContext)?.nextPage(), enabled: (commandContext) => stage(commandContext)?.canGoNext() ?? false, visible: manyPages, } , { id: 'page:previous', labelKey: 'commands.page.previous', icon: 'chevronLeft', shortcut: 'ArrowLeft', run: (commandContext) => stage(commandContext)?.previousPage(), enabled: (commandContext) => stage(commandContext)?.canGoPrevious() ?? false, visible: manyPages, } , { id: 'page:first', labelKey: 'commands.page.first', shortcut: 'Home', run: (commandContext) => stage(commandContext)?.goToFirstPage(), enabled: (commandContext) => stage(commandContext)?.canGoPrevious() ?? false, visible: manyPages, } , { id: 'page:last', labelKey: 'commands.page.last', shortcut: 'End', run: (commandContext) => stage(commandContext)?.goToLastPage(), enabled: (commandContext) => stage(commandContext)?.canGoNext() ?? false, visible: manyPages, } , { id: 'page:go-to', labelKey: 'commands.page.goTo', run: (commandContext) =>{ const view = stage(commandContext); if (!view) return; const page = pageArgOf(commandContext.args); if (page === null){ commandContext.tryGet(ShellToken)?.open(GO_TO_PAGE_DIALOG, { exclusive: 'modal'} ); return; } view.goToPage(Math.min(Math.max(page, 1), view.getPageCount()) - 1); } , enabled: (commandContext) => (stage(commandContext)?.getPageCount() ?? 0) > 0, } , { id: 'tool:pan', labelKey: 'commands.pan', icon: 'hand', run: (commandContext) => interaction(commandContext)?.activateTool('pan'), active: (commandContext) => interaction(commandContext)?.getActiveToolId() === 'pan', enabled: (commandContext) => interaction(commandContext) != null, } , { id: 'tool:pointer', labelKey: 'commands.pointer', icon: 'pointer', run: (commandContext) => interaction(commandContext)?.activateTool('pointer'), active: (commandContext) => interaction(commandContext)?.getActiveToolId() === 'pointer', enabled: (commandContext) => interaction(commandContext) != null, } , { id: 'tool:zoom', labelKey: 'commands.zoom.marquee', icon: 'zoomInArea', run: (commandContext) => interaction(commandContext)?.activateTool('zoom'), active: (commandContext) => interaction(commandContext)?.getActiveToolId() === 'zoom', enabled: (commandContext) => interaction(commandContext) != null, } , { id: 'tool:snapshot', labelKey: 'commands.snapshot.tool', icon: 'screenshot', categories: ['snapshots'], allowed: (commandContext) => renderOf(commandContext)?.canSnapshot() ?? true, notAllowedKey: 'commands.notAllowed.doc-content-copy', run: (commandContext) => interaction(commandContext)?.activateTool(SNAPSHOT_TOOL), active: (commandContext) => interaction(commandContext)?.getActiveToolId() === SNAPSHOT_TOOL, enabled: (commandContext) => interaction(commandContext)?.hasTool(SNAPSHOT_TOOL) ?? false, } , { id: 'menu:document', labelKey: 'commands.menu', icon: 'menu', menu: 'document', } , { id: 'menu:view', labelKey: 'commands.viewControls', icon: 'viewSettings', menu: 'view', } , ...outlineCommands, ...attachmentCommands, ...commentCommands, ...pageCommands, openFileCommand, { id: 'document:download', labelKey: 'commands.download', icon: 'download', shortcut: 'Mod+S', categories: ['documents.download'], allowed: (commandContext) => commandContext.tryGet(DocumentsToken)?.canDownload() ?? true, notAllowedKey: 'commands.notAllowed.doc-download', run: (commandContext) =>{ if (!commandContext.tryGet(DocumentsToken)) return; Promise.resolve(standardDownload.run?.(commandContext)).catch((error: unknown) => console.warn('[embedpdf] download failed', error), ); } , enabled: (commandContext) => commandContext.documentId != null, } , { id: 'document:print', labelKey: 'commands.print', icon: 'print', shortcut: 'Mod+P', categories: ['documents.print'], allowed: (commandContext) => commandContext.tryGet(DocumentsToken)?.canPrint() ?? true, notAllowedKey: 'commands.notAllowed.doc-print', modal: PRINT_DIALOG, enabled: (commandContext) => commandContext.documentId != null, } , spread('view:single-page', 'none', 'commands.spread.none', 'singlePage'), spread('view:two-pages', 'odd', 'commands.spread.odd', 'doublePage'), spread('view:cover-page', 'even', 'commands.spread.even', 'book2'), scroll('view:scroll-vertical', 'vertical', 'commands.scroll.vertical', 'vertical'), scroll('view:scroll-horizontal', 'horizontal', 'commands.scroll.horizontal', 'horizontal'), { id: 'view:scroll-paged', labelKey: 'commands.scroll.paged', icon: 'pageByPage', run: (commandContext) => stage(commandContext)?.updateSettings({ flow: 'paged'} ), active: (commandContext) => stage(commandContext)?.getSettings().flow === 'paged', enabled: (commandContext) => stage(commandContext) != null, locked: (commandContext) => stage(commandContext)?.getLock('flow') ?? null, } , { id: 'view:rotate-clockwise', labelKey: 'commands.rotate.clockwise', icon: 'rotateClockwise', run: (commandContext) => stage(commandContext)?.rotateViewBy(90), enabled: (commandContext) => stage(commandContext) != null, locked: (commandContext) => stage(commandContext)?.getLock('viewRotation') ?? null, } , { id: 'view:rotate-counterclockwise', labelKey: 'commands.rotate.counterclockwise', icon: 'rotateCounterClockwise', run: (commandContext) => stage(commandContext)?.rotateViewBy(-90), enabled: (commandContext) => stage(commandContext) != null, locked: (commandContext) => stage(commandContext)?.getLock('viewRotation') ?? null, } , { id: 'mode:view', labelKey: 'commands.mode.view', run: (commandContext) =>{ const shell = commandContext.tryGet(ShellToken); const open = shell ? openModeSurfaceOf(shell) : null; if (open) shell?.close(open); interaction(commandContext)?.activateTool('pointer'); } , active: (commandContext) =>{ const shell = commandContext.tryGet(ShellToken); return shell ? openModeSurfaceOf(shell) === null : true; } , } , modeCommand('mode:annotate', 'commands.mode.annotate', { feature: 'annotations'} ), modeCommand('mode:shapes', 'commands.mode.shapes', { feature: 'shapes'} ), modeCommand('mode:insert', 'commands.mode.insert'), modeCommand('mode:form', 'commands.mode.form', { feature: 'forms.building'} ), modeCommand('mode:redact', 'commands.mode.redact', { feature: 'redaction'} ), modeCommand('mode:measure', 'commands.mode.measure', { feature: 'measurement'} ), tool('tool:highlight', 'highlight', 'commands.annotate.highlight', 'highlight', { feature: 'annotations.highlight', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:strikeout', 'strikeout', 'commands.annotate.strikeout', 'strikethrough', { feature: 'annotations.markup', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:underline', 'underline', 'commands.annotate.underline', 'underline', { feature: 'annotations.markup', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:squiggly', 'squiggly', 'commands.annotate.squiggly', 'squiggly', { feature: 'annotations.markup', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:ink', 'ink', 'commands.annotate.ink', 'pencilMarker', { feature: 'annotations.drawing', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:ink-highlight', 'ink-highlight', 'commands.annotate.inkHighlight', 'inkHighlighter', { feature: 'annotations.drawing', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:free-text', 'free-text', 'commands.annotate.text', 'freeText', { feature: 'annotations.text', check: CREATES_ANNOTATIONS, accent:{ primary: 'fontColor'} , } ), tool('tool:insert-text', 'insert-text', 'commands.annotate.insertText', 'insertText', { feature: 'annotations.text', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:replace-text', 'replace-text', 'commands.annotate.replaceText', 'replaceText', { feature: 'annotations.text', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:callout', 'free-text-callout', 'commands.annotate.callout', 'callout', { feature: 'annotations.text', check: CREATES_ANNOTATIONS, accent:{ primary: 'color', secondary: 'interiorColor'} , } ), tool('tool:note', 'note', 'commands.annotate.note', 'message', { feature: 'annotations.notes', check: CREATES_ANNOTATIONS, } ), tool('tool:link', 'link', 'commands.annotate.link', 'link', { feature: 'annotations.links', check: CREATES_ANNOTATIONS, } ), tool('tool:square', 'square', 'commands.shapes.rectangle', 'square', { feature: 'shapes', check: CREATES_ANNOTATIONS, accent:{ primary: 'color', secondary: 'interiorColor'} , } ), tool('tool:circle', 'circle', 'commands.shapes.circle', 'circle', { feature: 'shapes', check: CREATES_ANNOTATIONS, accent:{ primary: 'color', secondary: 'interiorColor'} , } ), tool('tool:line', 'line', 'commands.shapes.line', 'line', { feature: 'shapes', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:arrow', 'arrow', 'commands.shapes.arrow', 'lineArrow', { feature: 'shapes', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), tool('tool:polygon', 'polygon', 'commands.shapes.polygon', 'polygon', { feature: 'shapes', check: CREATES_ANNOTATIONS, accent:{ primary: 'color', secondary: 'interiorColor'} , } ), tool('tool:polyline', 'polyline', 'commands.shapes.polyline', 'zigzag', { feature: 'shapes', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), { id: 'tool:stamp', labelKey: 'commands.insert.stamp', icon: 'rubberStamp', categories: ['stamps'], ...PLACES_STAMPS, ...togglesPanel('stamps', (commandContext) => commandContext.tryGet(StampToken) != null), } , tool('tool:attachment', 'attachment', 'commands.insert.attachment', 'paperclip', { feature: 'attachments.adding', check: CREATES_ANNOTATIONS, } ), { id: 'tool:signature', labelKey: 'commands.insert.signature', icon: 'signature', categories: ['signatures.signing'], ...PLACES_STAMPS, ...togglesPanel( 'signatures', (commandContext) => commandContext.tryGet(StampToken) != null && commandContext.tryGet(SignatureToken) != null, ), } , tool('tool:image', 'image', 'commands.insert.image', 'photo', { feature: 'stamps', check: CREATES_ANNOTATIONS, } ), { id: 'form:design', labelKey: 'commands.form.design', icon: 'formDesign', categories: ['forms.building'], ...DESIGNS_FORMS, run: (commandContext) =>{ const tools = interaction(commandContext); const shell = commandContext.tryGet(ShellToken); if (!tools) return; if (isDesigningForm(commandContext)){ tools.activateTool('pointer'); shell?.close(FORM_DESIGN_PANEL); return; } if (shell && !shell.isOpen('mode:form')) shell.open('mode:form', { exclusive: 'mode'} ); tools.activateTool(FORM_DESIGN_TOOL); shell?.open(FORM_DESIGN_PANEL); } , active: isDesigningForm, enabled: (commandContext) => interaction(commandContext) != null, } , tool('tool:text-field', 'form-text', 'commands.form.textfield', 'formTextfield', { feature: 'forms.building', check: DESIGNS_FORMS, } ), tool('tool:checkbox', 'form-checkbox', 'commands.form.checkbox', 'formCheckbox', { feature: 'forms.building', check: DESIGNS_FORMS, } ), tool('tool:radio', 'form-radio', 'commands.form.radio', 'formRadio', { feature: 'forms.building', check: DESIGNS_FORMS, } ), tool('tool:combo-box', 'form-combobox', 'commands.form.select', 'formSelect', { feature: 'forms.building', check: DESIGNS_FORMS, } ), tool('tool:list-box', 'form-listbox', 'commands.form.listbox', 'formListbox', { feature: 'forms.building', check: DESIGNS_FORMS, } ), tool('tool:button', 'form-button', 'commands.form.button', 'formButton', { feature: 'forms.building', check: DESIGNS_FORMS, } ), tool('tool:signature-field', 'form-signature', 'commands.form.signature', 'signature', { feature: 'forms.building', check: DESIGNS_FORMS, } ), tool('tool:redact', 'redact', 'commands.redact.mark', 'redactArea', { feature: 'redaction', check:{ allowed: (commandContext) => commandContext.tryGet(RedactionToken)?.canMark() ?? true, notAllowedKey: 'commands.notAllowed.annotations-create', } , } ), { id: 'redaction:apply', labelKey: 'commands.redact.applyAll', icon: 'redact', categories: ['redaction'], allowed: (commandContext) => commandContext.tryGet(RedactionToken)?.canApply() ?? true, modal: REDACT_CONFIRM_DIALOG, enabled: (commandContext) =>{ const redaction = commandContext.tryGet(RedactionToken); return !!redaction && redaction.getPendingCount() > 0 && !redaction.isApplying(); } , } , { id: 'annotation:apply-redaction', labelKey: 'commands.redact.apply', labelParams: (commandContext) => ({ count: selectedMarks(commandContext).length} ), icon: 'check', categories: ['redaction'], allowed: (commandContext) => commandContext.tryGet(RedactionToken)?.canApply() ?? true, run: (commandContext) => commandContext.tryGet(ShellToken)?.open(REDACT_CONFIRM_DIALOG, { exclusive: 'modal', props:{ refs: selectedMarks(commandContext)} , } ), visible: (commandContext) => selectedMarks(commandContext).length > 0, enabled: (commandContext) =>{ const redaction = commandContext.tryGet(RedactionToken); return !!redaction && !redaction.isApplying(); } , } , { ...tool('tool:distance', 'distance', 'measurement.distance', 'distance', { feature: 'measurement', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), enabled: canMeasureNow, } , { ...tool('tool:perimeter', 'perimeter', 'measurement.perimeter', 'perimeter', { feature: 'measurement', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), enabled: canMeasureNow, } , { ...tool('tool:area', 'area', 'measurement.area', 'area', { feature: 'measurement', check: CREATES_ANNOTATIONS, accent:{ primary: 'color'} , } ), enabled: canMeasureNow, } , { id: 'measurement:calibrate', labelKey: 'measurement.calibrate', icon: 'calibrate', categories: ['measurement'], allowed: (commandContext) => commandContext.tryGet(MeasurementToken)?.canCalibrate() ?? true, notAllowedKey: 'commands.notAllowed.doc-annotate-modify', enabled: (commandContext) => commandContext.tryGet(MeasurementToken) != null, active: (commandContext) => interaction(commandContext)?.getActiveToolId() === 'calibrate', run: (commandContext) => commandContext.tryGet(MeasurementToken)?.startCalibration(), } , { id: 'annotation:cancel-creation', labelKey: 'commands.annotate.cancelCreation', shortcut: 'Escape', enabled: (commandContext) => anno(commandContext)?.draft.get() != null, run: (commandContext) => anno(commandContext)?.draft.cancel(), } , { id: 'annotation:delete', labelKey: 'commands.annotate.delete', icon: 'trash', shortcut: ['Delete', 'Backspace'], categories: ['annotations'], run: (commandContext) => void anno(commandContext)?.selection.delete(), visible: hasAnnotationSelection, enabled: (commandContext) =>{ const annotation = anno(commandContext); const selected = annotation?.selection.getSelected() ?? []; return ( selected.length > 0 && selected.every(({ ref} ) => annotation!.canDelete(ref) && annotation!.isEditable(ref)) ); } , } , { id: 'annotation:comment', labelKey: 'commands.comment', icon: 'comment', categories: ['comments'], ...togglesPanel( 'comments', (commandContext) => hasAnnotationSelection(commandContext) && !selectionSubtypes(commandContext).has('widget'), ), } , { id: 'annotation:style', labelKey: 'commands.style', icon: 'palette', categories: ['annotations'], ...togglesPanel( 'style', (commandContext) => (anno(commandContext)?.selection.getProperties().properties.length ?? 0) > 0, ), } , { id: 'annotation:stamp-from-selection', labelKey: 'commands.annotate.stampFromSelection', icon: 'rubberStampPlus', categories: ['stamps'], allowed: (commandContext) => commandContext .tryGet(StampToken) ?.canCreateFromAnnotations(commandContext.documentId ?? undefined) ?? true, notAllowedKey: 'commands.notAllowed.doc-download', run: (commandContext) =>{ const annotation = anno(commandContext); const stamp = commandContext.tryGet(StampToken); const documentId = commandContext.documentId; if (!annotation || !stamp || documentId == null) return; const dtos = annotation.selection.getSelected(); const page = dtos[0]?.ref.page; if (page === undefined) return; const i18n = commandContext.tryGet(I18nToken); const label = i18n?.t('demo.stampsCustomLabel') ?? 'Custom stamp'; const libraryName = i18n?.t('demo.stampsCustomLibrary') ?? 'My stamps'; const libraryId = CUSTOM_LIBRARY_ID; const ensureLibrary = stamp.getLibrary(libraryId) ? Promise.resolve(libraryId) : stamp .createLibrary(libraryName, { id: libraryId, categories: ['custom']} ) .then(({ library} ) => library.id); ensureLibrary .then((id) => stamp.createAssetFromAnnotations( page, dtos.map((annotation) => annotation.ref), { libraryId: id, label: `${ label} ${ stamp.listAssets({ libraryId: id} ).length + 1} `} , { documentId} , ), ) .then(async () =>{ const shell = commandContext.tryGet(ShellToken); if (!shell) return; if (!shell.isOpen('stamps')){ await commandContext .tryGet(CommandsToken) ?.execute(panelCommandOf('stamps'), { documentId} ); } shell.updateSurfaceProps('stamps', { libraryId} ); } ) .catch((error) => console.warn('[embedpdf] stamp from selection failed', error)); } , visible: (commandContext) => commandContext.tryGet(StampToken) != null && hasAnnotationSelection(commandContext) && !selectionSubtypes(commandContext).has('widget') && !selectionSubtypes(commandContext).has('redact') && new Set( (anno(commandContext)?.selection.getSelected() ?? []).map( (annotation) => annotation.ref.page.objectNumber, ), ).size === 1, } , { id: 'annotation:duplicate', labelKey: 'commands.annotate.duplicate', icon: 'duplicate', shortcut: 'Mod+D', categories: ['annotations'], allowed: (commandContext) =>{ const annotation = anno(commandContext); const own = (annotation?.selection.getSelected() ?? []).some( ({ subtype} ) => subtype !== 'widget', ); return own ? (annotation?.canCreate() ?? true) : true; } , notAllowedKey: CREATES_ANNOTATIONS.notAllowedKey, run: (commandContext) => void anno(commandContext) ?.selection.duplicate() .catch(() =>{ } ), visible: (commandContext) =>{ const annotation = anno(commandContext); const selected = annotation?.selection.getSelected() ?? []; return ( selected.length > 0 && selectionEditable(commandContext) && selected.every(({ ref} ) => annotation!.isMovable(ref)) ); } , enabled: (commandContext) => anno(commandContext)?.selection.canDuplicate() ?? false, } , { id: 'annotation:rotate-counterclockwise', labelKey: 'commands.annotate.rotateCounterclockwise', icon: 'rotateCounterClockwise', categories: ['annotations'], run: (commandContext) => void anno(commandContext) ?.selection.rotateBy(-90) .catch(() =>{ } ), visible: selectionTurnsByQuarters, } , { id: 'annotation:rotate-clockwise', labelKey: 'commands.annotate.rotateClockwise', icon: 'rotateClockwise', categories: ['annotations'], run: (commandContext) => void anno(commandContext) ?.selection.rotateBy(90) .catch(() =>{ } ), visible: selectionTurnsByQuarters, } , { id: 'annotation:group', labelKey: 'commands.annotate.group', icon: 'group', categories: ['annotations'], run: (commandContext) => void anno(commandContext)?.selection.group(), visible: (commandContext) => (anno(commandContext)?.selection.canGroup() ?? false) && selectionEditable(commandContext), } , { id: 'annotation:ungroup', labelKey: 'commands.annotate.ungroup', icon: 'ungroup', categories: ['annotations'], run: (commandContext) => void anno(commandContext)?.selection.ungroup(), visible: (commandContext) => (anno(commandContext)?.selection.canUngroup() ?? false) && selectionEditable(commandContext), } , { id: 'selection:copy', labelKey: 'commands.selection.copy', icon: 'copy', categories: ['text.copy'], allowed: (commandContext) => textSelection(commandContext)?.canCopy() ?? true, notAllowedKey: 'commands.notAllowed.doc-text-copy', visible: (commandContext) => textSelection(commandContext)?.hasSelection() ?? false, run: (commandContext) =>{ const selection = textSelection(commandContext); if (!selection) return; const copiedRange = selection.getRange(); void copySelection(selection).then( (text) =>{ if (text !== '' && sameTextRange(selection.getRange(), copiedRange)) selection.clear(); } , () =>{ } , ); } , } , markupSelection( 'selection:highlight', 'highlight', 'commands.annotate.highlight', 'highlight', 'annotations.highlight', ), markupSelection( 'selection:underline', 'underline', 'commands.annotate.underline', 'underline', 'annotations.markup', ), markupSelection( 'selection:strikeout', 'strikeout', 'commands.annotate.strikeout', 'strikethrough', 'annotations.markup', ), { id: 'snapshot:copy', labelKey: 'commands.snapshot.copy', icon: 'copy', shortcut: 'Mod+C', categories: ['snapshots'], visible: (commandContext) => snapshotOf(commandContext) != null, run: (commandContext) =>{ const render = renderOf(commandContext); const snapshot = render?.getSnapshot(); if (!render || !snapshot) return; void copyImage(snapshot.image).then( () =>{ if (render.getSnapshot() === snapshot) render.clearSnapshot(); } , () =>{ } , ); } , } , { id: 'snapshot:save', labelKey: 'commands.snapshot.save', icon: 'download', categories: ['snapshots'], visible: (commandContext) => snapshotOf(commandContext) != null, run: (commandContext) =>{ const snapshot = snapshotOf(commandContext); if (!snapshot) return; const name = snapshotFileName(commandContext, snapshot.page); snapshot.image .then((image) => image.blob()) .then((blob) => saveFile(blob, name, 'image/png')) .catch((error) => console.warn('[snippet-react] snapshot save failed', error)); } , } , { id: 'snapshot:clear', labelKey: 'commands.snapshot.clear', icon: 'x', shortcut: 'Escape', categories: ['snapshots'], visible: (commandContext) => snapshotOf(commandContext) != null, run: (commandContext) => renderOf(commandContext)?.clearSnapshot(), } , { id: 'annotation:link', labelKey: 'commands.annotate.link', icon: 'link', categories: ['annotations.links'], run: (commandContext) => commandContext.tryGet(ShellToken)?.toggle('link-editor'), active: (commandContext) => commandContext.tryGet(ShellToken)?.isOpen('link-editor') ?? false, visible: (commandContext) => selectionLink(commandContext) === null, } , { id: 'annotation:goto-link', labelKey: 'commands.annotate.gotoLink', icon: 'externalLink', run: (commandContext) =>{ const target = selectionLink(commandContext); if (target) commandContext.tryGet(LinkToken)?.activate(target); } , visible: (commandContext) => selectionLink(commandContext) != null, } , { id: 'annotation:remove-link', labelKey: 'commands.annotate.removeLink', icon: 'linkOff', categories: ['annotations.links'], run: (commandContext) => anno(commandContext)?.selection.updateLink(null), visible: (commandContext) => selectionLink(commandContext) != null, } , { id: 'help:shortcuts', labelKey: 'commands.shortcuts', shortcut: '?', modal: SHORTCUTS_DIALOG, enabled: (commandContext) => commandContext.tryGet(ShellToken) != null, } , { id: 'history:undo', labelKey: 'commands.undo', icon: 'arrowBackUp', shortcut: 'Mod+z', run: (commandContext) => commandContext.tryGet(HistoryToken)?.undo(), enabled: (commandContext) => commandContext.tryGet(HistoryToken)?.canUndo() ?? false, } , { id: 'history:redo', labelKey: 'commands.redo', icon: 'arrowForwardUp', shortcut: ['Shift+Mod+z', 'Mod+y'], run: (commandContext) => commandContext.tryGet(HistoryToken)?.redo(), enabled: (commandContext) => commandContext.tryGet(HistoryToken)?.canRedo() ?? false, } , ] isDesigningForm [VariableDeclaration]: isDesigningForm = (commandContext: Pick): boolean => isFormDesignTool(commandContext.tryGet(InteractionToken)?.getActiveToolId()) isFormDesignTool [VariableDeclaration]: isFormDesignTool = (tool: string | null | undefined): boolean => tool === FORM_DESIGN_TOOL || (tool != null && FORM_FIELD_TOOLS.includes(tool)) toolCommandsFor [FunctionDeclaration]: export function toolCommandsFor( tools: readonly AnnotationToolInput[], isTaken: (id: string) => boolean, ): CommandDefinition[]{ return tools.flatMap((input): CommandDefinition[] =>{ const id = `tool:${ input.id} `; if (isTaken(id)) return []; const base = input.extends; const meta = input.meta ??{ } ; const icon = typeof meta.icon === 'string' ? meta.icon : base && TOOL_ICONS[base]?.icon; const command = toolCommand(id, input.id, icon || undefined, { feature: (base && TOOL_FEATURES[base]) || 'annotations', check: CREATES_ANNOTATIONS, ...(base && TOOL_ICONS[base]?.accent ?{ accent: TOOL_ICONS[base].accent} :{ } ), } ); return [{ ...command, label: typeof meta.label === 'string' ? meta.label : input.id} ]; } ); }