Reimplement tagjoin with tick()

This commit is contained in:
Henrik Giesel 2021-06-26 01:00:41 +02:00
parent 180ef140f8
commit fe35573308
2 changed files with 18 additions and 28 deletions

View File

@ -56,36 +56,24 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
tags = tags;
}
function joinWithPreviousTag(
index: number,
setPosition: (position: number) => void
): void {
function joinWithPreviousTag(index: number): void {
if (index === 0) {
return;
}
const spliced = tags.splice(index - 1, 1)[0];
const length = spliced.name.length;
tags[index - 1].name = spliced.name + tags[index - 1].name;
tags = tags;
setPosition(length);
}
function joinWithNextTag(
index: number,
setPosition: (position: number) => void
): void {
function joinWithNextTag(index: number): void {
if (index === tags.length - 1) {
return;
}
const spliced = tags.splice(index + 1, 1)[0];
const length = tags[index].name.length;
tags[index].name = tags[index].name + spliced.name;
tags = tags;
setPosition(length);
}
function moveToPreviousTag(index: number): void {
@ -115,13 +103,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
newName = "";
}
function joinWithLastTag(setPosition: (position: number) => void): void {
function joinWithLastTag(): void {
const popped = tags.pop();
tags = tags;
if (popped) {
newName = popped.name + newName;
setPosition(popped.name.length);
}
}
@ -149,10 +136,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:tagupdate={() => checkForDuplicateAt(index)}
on:tagadd={() => insertTagAt(index)}
on:tagdelete={() => deleteTagAt(index)}
on:tagjoinprevious={({ detail }) =>
joinWithPreviousTag(index, detail.setPosition)}
on:tagjoinnext={({ detail }) =>
joinWithNextTag(index, detail.setPosition)}
on:tagjoinprevious={() => joinWithPreviousTag(index)}
on:tagjoinnext={() => joinWithNextTag(index)}
on:tagmoveprevious={() => moveToPreviousTag(index)}
on:tagmovenext={() => moveToNextTag(index)}
/>
@ -165,8 +150,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:blur={destroyAutocomplete}
on:tagupdate={appendTag}
on:tagadd={appendTag}
on:tagjoinprevious={({ detail }) =>
joinWithLastTag(detail.setPosition)}
on:tagjoinprevious={joinWithLastTag}
on:tagmoveprevious={moveToLastTag}
/>
</TagAutocomplete>

View File

@ -3,7 +3,7 @@ Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="typescript">
import { createEventDispatcher, onMount } from "svelte";
import { createEventDispatcher, onMount, tick } from "svelte";
import { normalizeTagname } from "./tags";
export let name: string;
@ -23,7 +23,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}
function setPosition(position: number): void {
setTimeout(() => input.setSelectionRange(position, position));
input.setSelectionRange(position, position);
}
function onAccept(): void {
@ -31,9 +31,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
dispatch("tagupdate", { name });
}
function onBackspace(event: KeyboardEvent) {
async function onBackspace(event: KeyboardEvent): Promise<void> {
if (caretAtStart()) {
dispatch("tagjoinprevious", { setPosition });
const length = input.value.length;
dispatch("tagjoinprevious");
await tick();
setPosition(input.value.length - length);
event.preventDefault();
} else if (name.endsWith("::")) {
name = name.slice(0, -2);
@ -41,9 +44,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}
}
function onDelete(event: KeyboardEvent) {
async function onDelete(event: KeyboardEvent): Promise<void> {
if (caretAtEnd()) {
dispatch("tagjoinnext", { setPosition });
const length = input.value.length;
dispatch("tagjoinnext");
await tick();
setPosition(length);
event.preventDefault();
} else if (name.endsWith("::")) {
name = name.slice(0, -2);