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 {
tags.splice(index, 1);
function deleteTag(tag: TagType, index: number): TagType {
const deleted = tags.splice(index, 1)[0];
tags = tags;
active = null;
updateActiveAfterBlur((active: number) => {
if (active === index) {
return null;
} else if (active > index) {
return active - 1;
if (active !== null && active > index) {
active--;
}
return 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;
return deleted;
}
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;
}
const spliced = tags.splice(index - 1, 1)[0];
tags[index - 1].name = spliced.name + tags[index - 1].name;
const deleted = deleteTag(
tag /* invalid, probably need to change signature of deleteTag */,
index - 1
);
tag.name = deleted.name + tag.name;
tags = tags;
console.log(active, activeAfterBlur);
}
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);
}
async function activate(index: number): Promise<void> {
function activate(index: number): void {
active = index;
}
async function checkForActivation(index: number): Promise<void> {
const selection = window.getSelection()!;
if (selection.isCollapsed) {
await activate(index);
}
}
</script>
<StickyBottom>
@ -201,7 +207,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:blur={decideNextActive}
on:tagenter={() => enterTag(tag, index)}
on:tagadd={() => insertTag(tag, index)}
on:tagdelete={() => deleteTag(tag, index)}
on:tagdelete={() => deleteActiveTag(tag, index)}
on:tagunique={() => deleteTagIfNotUnique(tag, index)}
on:tagjoinprevious={() => joinWithPreviousTag(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
name={tag.name}
bind:blink={tag.blink}
on:click={() => checkForActivation(index)}
on:click={() => activate(index)}
on:tagdelete={() => deleteTag(tag, index)}
/>
{/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> {
const length = input.value.length;
dispatch("tagjoinprevious");
await tick();
setPosition(input.value.length - length);
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 {
event.preventDefault();
normalize();
console.log("taginput onblur");
if (name.length === 0) {
dispatch("tagdelete");
}
@ -95,6 +97,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
// TODO
name += "::";
event.preventDefault();
break;
case "Backspace":
onBackspace(event);
@ -111,8 +114,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
if (isEmpty()) {
joinWithPreviousTag(event);
} else {
dispatch("tagmoveprevious");
event.preventDefault();
dispatch("tagmoveprevious");
}
break;
@ -123,9 +126,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
if (isEmpty()) {
joinWithNextTag(event);
} else {
dispatch("tagmovenext");
event.preventDefault();
dispatch("tagmovenext");
}
break;
}
}