Make focus and folding robust against reordering

Now paths use the same keys as vue's key property instead of being
purely index-based.
This commit is contained in:
Joscha 2025-02-07 22:59:51 +01:00
parent 363f88edcf
commit e35124f5ac
2 changed files with 16 additions and 13 deletions

View file

@ -28,12 +28,12 @@ const note = computed(() => notes.notes.get(props.noteId));
const children = computed(() => { const children = computed(() => {
if (note.value === undefined) return []; if (note.value === undefined) return [];
const seen = new Map<string, number>(); const seen = new Map<string, number>();
const children: [string, string][] = []; const children: { id: string; key: string }[] = [];
for (const id of note.value.children) { for (const id of note.value.children) {
const n = seen.get(id) || 0; const n = seen.get(id) || 0;
seen.set(id, n + 1); seen.set(id, n + 1);
const key = `${id}-${n}`; const key = `${id}-${n}`;
children.push([id, key]); children.push({ id, key });
} }
return children; return children;
}); });
@ -107,7 +107,10 @@ function onCreateEditorClose() {
function onCreateEditorFinish(text: string) { function onCreateEditorFinish(text: string) {
notes.appendNewChildNote(props.noteId, text); notes.appendNewChildNote(props.noteId, text);
ui.focusPath = pathAppend(props.path, children.value.length - 1);
const lastChild = children.value.at(-1);
if (lastChild) ui.focusPath = pathAppend(props.path, lastChild.key);
onCreateEditorClose(); onCreateEditorClose();
} }
</script> </script>
@ -163,10 +166,10 @@ function onCreateEditorFinish(text: string) {
<!-- Children --> <!-- Children -->
<div v-if="open && children.length > 0" class="flex flex-col pl-2"> <div v-if="open && children.length > 0" class="flex flex-col pl-2">
<CNote <CNote
v-for="([noteId, key], index) in children" v-for="child in children"
:key :key="child.key"
:note-id :noteId="child.id"
:path="pathAppend(path, index)" :path="pathAppend(path, child.key)"
/> />
</div> </div>

View file

@ -1,14 +1,14 @@
function pathString(path: number[]): string { function pathString(keys: string[]): string {
return path.join("/"); return keys.join("/");
} }
function pathParse(path: string): number[] { function pathParse(path: string): string[] {
if (path === "") return []; if (path === "") return [];
return path.split("/").map((it) => Number.parseInt(it)); return path.split("/");
} }
export function pathAppend(path: string, segment: number): string { export function pathAppend(path: string, key: string): string {
return pathString(pathParse(path).concat(segment)); return pathString(pathParse(path).concat(key));
} }
export function pathAncestor(path: string): string { export function pathAncestor(path: string): string {