Implement tagsplit

This commit is contained in:
Henrik Giesel 2021-06-27 03:46:42 +02:00
parent de10e55265
commit 3857ac07d7
2 changed files with 21 additions and 8 deletions

View file

@ -58,8 +58,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
active = index; active = index;
} }
function appendEmptyTagAt(index: number): void { function appendTagAt(index: number, name: string): void {
tags.splice(index + 1, 0, attachId("")); tags.splice(index + 1, 0, attachId(name));
tags = tags; tags = tags;
setActiveAfterBlur(index + 1); setActiveAfterBlur(index + 1);
} }
@ -77,8 +77,21 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
return true; return true;
} }
function enterTag(_tag: TagType, index: number): void { async function splitTag(
appendEmptyTagAt(index); tag: TagType,
index: number,
start: number,
end: number
): Promise<void> {
const current = tag.name.slice(0, start);
const splitOff = tag.name.slice(end);
tag.name = current;
appendTagAt(index, splitOff);
active = null;
await tick();
input.setSelectionRange(0, 0);
} }
function insertTag(tag: TagType, index: number): void { function insertTag(tag: TagType, index: number): void {
@ -162,7 +175,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
async function moveToNextTag(tag: TagType, index: number): Promise<void> { async function moveToNextTag(tag: TagType, index: number): Promise<void> {
if (isLast(index)) { if (isLast(index)) {
if (tag.name.length !== 0) { if (tag.name.length !== 0) {
appendEmptyTagAt(index); appendTagAt(index, "");
active = null; active = null;
activeAfterBlur = index + 1; activeAfterBlur = index + 1;
} }
@ -206,7 +219,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
JSON.stringify(tags) JSON.stringify(tags)
)} )}
on:blur={decideNextActive} on:blur={decideNextActive}
on:tagenter={() => enterTag(tag, index)} on:tagsplit={({ detail }) =>
splitTag(tag, index, detail.start, detail.end)}
on:tagadd={() => insertTag(tag, index)} on:tagadd={() => insertTag(tag, index)}
on:tagdelete={() => deleteActiveTag(tag, index)} on:tagdelete={() => deleteActiveTag(tag, index)}
on:tagunique={() => deleteTagIfNotUnique(tag, index)} on:tagunique={() => deleteTagIfNotUnique(tag, index)}

View file

@ -86,8 +86,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
function onEnter(event: Event): void { function onEnter(event: Event): void {
event.preventDefault(); event.preventDefault();
dispatch("tagenter"); dispatch("tagsplit", { start: input.selectionStart, end: input.selectionEnd });
input.blur();
} }
function onKeydown(event: KeyboardEvent): void { function onKeydown(event: KeyboardEvent): void {