From fe4ba874903769ae0fd4007953a11c3dabe8f484 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Thu, 8 Apr 2021 23:08:36 +0200 Subject: [PATCH] Move cloze logic and wrapping logic editor-toolbar --- ts/editor-toolbar/cloze.ts | 46 +++++++++++++++++++++++++++++++++++ ts/editor-toolbar/template.ts | 27 ++++++-------------- 2 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 ts/editor-toolbar/cloze.ts diff --git a/ts/editor-toolbar/cloze.ts b/ts/editor-toolbar/cloze.ts new file mode 100644 index 000000000..5fe0f55f7 --- /dev/null +++ b/ts/editor-toolbar/cloze.ts @@ -0,0 +1,46 @@ +import IconButton from "./IconButton.svelte"; +import type { IconButtonProps } from "./IconButton"; + +import { dynamicComponent } from "sveltelib/dynamicComponent"; +import * as tr from "anki/i18n"; + +import bracketsIcon from "./code-brackets.svg"; + +const clozePattern = /\{\{c(\d+)::/gu; +function getCurrentHighestCloze(increment: boolean): number { + let highest = 0; + + // @ts-expect-error + forEditorField([], (field) => { + const matches = field.editingArea.editable.fieldHTML.matchAll(clozePattern); + highest = Math.max( + highest, + ...[...matches].map((match: RegExpMatchArray): number => Number(match[1])) + ); + }); + + if (increment) { + highest++; + } + + return Math.max(1, highest); +} + +function onCloze(event: MouseEvent): void { + const highestCloze = getCurrentHighestCloze(!event.altKey); + + // @ts-expect-error + wrap(`{{c${highestCloze}::`, "}}"); +} + +const iconButton = dynamicComponent(IconButton); +export const clozeButton = iconButton( + { + id: "cloze", + icon: bracketsIcon, + onClick: onCloze, + }, + { + tooltip: tr.editingClozeDeletionCtrlandshiftandc, + } +); diff --git a/ts/editor-toolbar/template.ts b/ts/editor-toolbar/template.ts index cecb7337f..2fae678d7 100644 --- a/ts/editor-toolbar/template.ts +++ b/ts/editor-toolbar/template.ts @@ -15,10 +15,11 @@ import * as tr from "anki/i18n"; import paperclipIcon from "./paperclip.svg"; import micIcon from "./mic.svg"; -import bracketsIcon from "./code-brackets.svg"; import functionIcon from "./function-variant.svg"; import xmlIcon from "./xml.svg"; +import { clozeButton } from "./cloze"; + function onAttachment(): void { bridgeCommand("attach"); } @@ -27,10 +28,6 @@ function onRecord(): void { bridgeCommand("record"); } -function onCloze(): void { - bridgeCommand("cloze"); -} - function onHtmlEdit(): void { bridgeCommand("htmlEdit"); } @@ -58,17 +55,6 @@ const recordButton = iconButton( } ); -const clozeButton = iconButton( - { - id: "cloze", - icon: bracketsIcon, - onClick: onCloze, - }, - { - tooltip: tr.editingClozeDeletionCtrlandshiftandc, - } -); - const mathjaxButton = iconButton>( { icon: functionIcon, @@ -84,7 +70,8 @@ const mathjaxMenu = dropdownMenu( menuItems: [ dropdownItem( { - onClick: () => bridgeCommand("mathjaxInline"), + // @ts-expect-error + onClick: () => wrap("\\(", "\\)"), tooltip: "test", endLabel: "test", }, @@ -92,7 +79,8 @@ const mathjaxMenu = dropdownMenu( ), dropdownItem( { - onClick: () => bridgeCommand("mathjaxBlock"), + // @ts-expect-error + onClick: () => wrap("\\[", "\\]"), tooltip: "test", endLabel: "test", }, @@ -100,7 +88,8 @@ const mathjaxMenu = dropdownMenu( ), dropdownItem( { - onClick: () => bridgeCommand("mathjaxChemistry"), + // @ts-expect-error + onClick: () => wrap("\\(\\ce{", "}\\)"), tooltip: "test", endLabel: "test", },