Fix tagjoinprevious

This commit is contained in:
Henrik Giesel 2021-06-27 02:58:54 +02:00
parent bfeb419ba6
commit 1487ed64c7
2 changed files with 37 additions and 27 deletions

View file

@ -90,20 +90,29 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
} }
function deleteTag(tag: TagType, index: number): void { function deleteTag(tag: TagType, index: number): TagType {
tags.splice(index, 1); const deleted = tags.splice(index, 1)[0];
tags = tags; tags = tags;
if (active !== null && active > index) {
active--;
}
return deleted;
}
function deleteActiveTag(tag: TagType, index: number): TagType {
const deleted = tags.splice(index, 1)[0];
tags = tags;
if (activeAfterBlur === index) {
activeAfterBlur = null;
} else if (activeAfterBlur !== null && activeAfterBlur > index) {
activeAfterBlur--;
}
active = null; active = null;
return deleted;
updateActiveAfterBlur((active: number) => {
if (active === index) {
return null;
} else if (active > index) {
return active - 1;
}
return active;
});
} }
function deleteTagIfNotUnique(tag: TagType, index: number): void { function deleteTagIfNotUnique(tag: TagType, index: number): void {
@ -122,9 +131,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
return; return;
} }
const spliced = tags.splice(index - 1, 1)[0]; const deleted = deleteTag(
tags[index - 1].name = spliced.name + tags[index - 1].name; tag /* invalid, probably need to change signature of deleteTag */,
index - 1
);
tag.name = deleted.name + tag.name;
tags = tags; tags = tags;
console.log(active, activeAfterBlur);
} }
function joinWithNextTag(tag: TagType, index: number): void { function joinWithNextTag(tag: TagType, index: number): void {
@ -162,16 +175,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
input.setSelectionRange(0, 0); input.setSelectionRange(0, 0);
} }
async function activate(index: number): Promise<void> { function activate(index: number): void {
active = index; active = index;
} }
async function checkForActivation(index: number): Promise<void> {
const selection = window.getSelection()!;
if (selection.isCollapsed) {
await activate(index);
}
}
</script> </script>
<StickyBottom> <StickyBottom>
@ -201,7 +207,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:blur={decideNextActive} on:blur={decideNextActive}
on:tagenter={() => enterTag(tag, index)} on:tagenter={() => enterTag(tag, index)}
on:tagadd={() => insertTag(tag, index)} on:tagadd={() => insertTag(tag, index)}
on:tagdelete={() => deleteTag(tag, index)} on:tagdelete={() => deleteActiveTag(tag, index)}
on:tagunique={() => deleteTagIfNotUnique(tag, index)} on:tagunique={() => deleteTagIfNotUnique(tag, index)}
on:tagjoinprevious={() => joinWithPreviousTag(tag, index)} on:tagjoinprevious={() => joinWithPreviousTag(tag, index)}
on:tagjoinnext={() => joinWithNextTag(tag, index)} on:tagjoinnext={() => joinWithNextTag(tag, index)}
@ -212,7 +218,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<Tag <Tag
name={tag.name} name={tag.name}
bind:blink={tag.blink} bind:blink={tag.blink}
on:click={() => checkForActivation(index)} on:click={() => activate(index)}
on:tagdelete={() => deleteTag(tag, index)} on:tagdelete={() => deleteTag(tag, index)}
/> />
{/if} {/if}

View file

@ -38,8 +38,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
async function joinWithPreviousTag(event: Event): Promise<void> { async function joinWithPreviousTag(event: Event): Promise<void> {
const length = input.value.length; const length = input.value.length;
dispatch("tagjoinprevious"); dispatch("tagjoinprevious");
await tick(); await tick();
setPosition(input.value.length - length); setPosition(input.value.length - length);
event.preventDefault(); event.preventDefault();
} }
@ -72,7 +74,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
function onBlur(event: Event): void { function onBlur(event: Event): void {
event.preventDefault(); event.preventDefault();
normalize(); normalize();
console.log("taginput onblur");
if (name.length === 0) { if (name.length === 0) {
dispatch("tagdelete"); dispatch("tagdelete");
} }
@ -95,6 +97,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
// TODO // TODO
name += "::"; name += "::";
event.preventDefault(); event.preventDefault();
break;
case "Backspace": case "Backspace":
onBackspace(event); onBackspace(event);
@ -111,8 +114,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
if (isEmpty()) { if (isEmpty()) {
joinWithPreviousTag(event); joinWithPreviousTag(event);
} else { } else {
dispatch("tagmoveprevious");
event.preventDefault(); event.preventDefault();
dispatch("tagmoveprevious");
} }
break; break;
@ -123,9 +126,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
if (isEmpty()) { if (isEmpty()) {
joinWithNextTag(event); joinWithNextTag(event);
} else { } else {
dispatch("tagmovenext");
event.preventDefault(); event.preventDefault();
dispatch("tagmovenext");
} }
break;
} }
} }