From f724b45ae11ab41c56a0a00a1908b2de52f91b77 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Wed, 5 May 2021 15:12:02 +0200 Subject: [PATCH] Add hiding functionality in ButtonGroup --- ts/components/ButtonGroup.svelte | 51 ++++++++++++++++----- ts/components/ButtonGroupItem.svelte | 4 +- ts/components/identifier.ts | 68 ++++++++++++++++++++-------- ts/editor/FormatInlineButtons.svelte | 2 +- ts/editor/NoteTypeButtons.svelte | 33 ++++++++------ 5 files changed, 113 insertions(+), 45 deletions(-) diff --git a/ts/components/ButtonGroup.svelte b/ts/components/ButtonGroup.svelte index 421f3b107..0d5388687 100644 --- a/ts/components/ButtonGroup.svelte +++ b/ts/components/ButtonGroup.svelte @@ -3,21 +3,21 @@ Copyright: Ankitects Pty Ltd and contributors License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html --> diff --git a/ts/components/identifier.ts b/ts/components/identifier.ts index 7d29362c6..deee14436 100644 --- a/ts/components/identifier.ts +++ b/ts/components/identifier.ts @@ -2,54 +2,86 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html export type Identifier = string | number; -function find(collection: HTMLCollection, idOrIndex: Identifier): Element | null { - let element: Element | null = null; +export function find( + collection: HTMLCollection, + idOrIndex: Identifier +): [number, Element] | null { + let result: [number, Element] | null = null; if (typeof idOrIndex === "string") { - element = collection.namedItem(idOrIndex); + const element = collection.namedItem(idOrIndex); + + if (element) { + const index = Array.prototype.indexOf.call(collection, element); + result = [index, element]; + } } else if (idOrIndex < 0) { - const normalizedIndex = collection.length + idOrIndex; - element = collection.item(normalizedIndex); + const index = collection.length + idOrIndex; + const element = collection.item(index); + + if (element) { + result = [index, element]; + } } else { - element = collection.item(idOrIndex); + const index = idOrIndex; + const element = collection.item(index); + + if (element) { + result = [index, element]; + } } - return element; + return result; } export function insert( element: Element, collection: Element, idOrIndex: Identifier -): void { - const reference = find(collection.children, idOrIndex); +): number { + const match = find(collection.children, idOrIndex); - if (reference) { - collection.insertBefore(element, reference); + if (match) { + const [index, reference] = match; + collection.insertBefore(element, reference[0]); + + return index; } + + return -1; } export function add( element: Element, collection: Element, idOrIndex: Identifier -): void { - const before = find(collection.children, idOrIndex); +): number { + const match = find(collection.children, idOrIndex); - if (before) { + if (match) { + const [index, before] = match; const reference = before.nextElementSibling ?? null; collection.insertBefore(element, reference); + + return index + 1; } + + return -1; } export function update( f: (element: Element) => void, collection: Element, idOrIndex: Identifier -): void { - const element = find(collection.children, idOrIndex); +): number { + const match = find(collection.children, idOrIndex); - if (element) { - f(element); + if (match) { + const [index, element] = match; + f(element[0]); + + return index; } + + return -1; } diff --git a/ts/editor/FormatInlineButtons.svelte b/ts/editor/FormatInlineButtons.svelte index e800c58b2..ce41cd072 100644 --- a/ts/editor/FormatInlineButtons.svelte +++ b/ts/editor/FormatInlineButtons.svelte @@ -5,8 +5,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - - bridgeCommand('fields')}> - {tr.editingFields()}... - - - + + bridgeCommand('cards')} - on:mount={createShortcut}> - {tr.editingCards()}... + tooltip={tr.editingCustomizeFields()} + on:click={() => bridgeCommand('fields')}> + {tr.editingFields()}... - + + + + + bridgeCommand('cards')} + on:mount={createShortcut}> + {tr.editingCards()}... + + +