From ac6869169adce8706b017dfa472b23b6151de09b Mon Sep 17 00:00:00 2001 From: BearToCode Date: Thu, 15 Feb 2024 11:37:12 +0100 Subject: [PATCH] chore: use custom attribute instead of id --- README.md | 2 +- packages/core/README.md | 2 +- .../src/lib/components/blocks/AddedBlock.svelte | 2 +- .../components/blocks/AddedBlockPlaceholder.svelte | 2 +- .../lib/components/blocks/MergeConflictBlock.svelte | 2 +- .../blocks/MergeConflictPlaceholder.svelte | 2 +- .../src/lib/components/blocks/ModifiedBlock.svelte | 2 +- .../src/lib/components/blocks/RemovedBlock.svelte | 2 +- .../blocks/RemovedBlockPlaceholder.svelte | 2 +- .../components/blocks/ResolvedConflictBlock.svelte | 2 +- .../blocks/ResolvedConflictPlaceholder.svelte | 2 +- .../src/lib/components/blocks/UnchangedBlock.svelte | 2 +- packages/core/src/lib/internal/editor/component.ts | 2 +- packages/core/src/lib/internal/editor/connection.ts | 6 +++--- packages/core/src/lib/internal/editor/merging.ts | 13 ++++++++++--- 15 files changed, 26 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 5f9e600..675d502 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Here is a basic explanation of how the the rendered html looks like:
-
+
diff --git a/packages/core/README.md b/packages/core/README.md index 5f40ab8..2d43677 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -103,7 +103,7 @@ Here is a basic explanation of how the the rendered html looks like:
-
+
diff --git a/packages/core/src/lib/components/blocks/AddedBlock.svelte b/packages/core/src/lib/components/blocks/AddedBlock.svelte index b19a4ed..661eeda 100644 --- a/packages/core/src/lib/components/blocks/AddedBlock.svelte +++ b/packages/core/src/lib/components/blocks/AddedBlock.svelte @@ -8,7 +8,7 @@ export let lines: Line[]; -
+
{#each lines as line}
diff --git a/packages/core/src/lib/components/blocks/AddedBlockPlaceholder.svelte b/packages/core/src/lib/components/blocks/AddedBlockPlaceholder.svelte index 9624c1b..80ea8e8 100644 --- a/packages/core/src/lib/components/blocks/AddedBlockPlaceholder.svelte +++ b/packages/core/src/lib/components/blocks/AddedBlockPlaceholder.svelte @@ -6,4 +6,4 @@ export let component: BlockComponent; -
+
diff --git a/packages/core/src/lib/components/blocks/MergeConflictBlock.svelte b/packages/core/src/lib/components/blocks/MergeConflictBlock.svelte index d279c75..a71af4f 100644 --- a/packages/core/src/lib/components/blocks/MergeConflictBlock.svelte +++ b/packages/core/src/lib/components/blocks/MergeConflictBlock.svelte @@ -8,7 +8,7 @@ export let component: BlockComponent; -
+
{#each lines as line}
diff --git a/packages/core/src/lib/components/blocks/MergeConflictPlaceholder.svelte b/packages/core/src/lib/components/blocks/MergeConflictPlaceholder.svelte index ee67da0..8d32fe2 100644 --- a/packages/core/src/lib/components/blocks/MergeConflictPlaceholder.svelte +++ b/packages/core/src/lib/components/blocks/MergeConflictPlaceholder.svelte @@ -6,4 +6,4 @@ export let component: BlockComponent; -
+
diff --git a/packages/core/src/lib/components/blocks/ModifiedBlock.svelte b/packages/core/src/lib/components/blocks/ModifiedBlock.svelte index 9fcab71..c607b01 100644 --- a/packages/core/src/lib/components/blocks/ModifiedBlock.svelte +++ b/packages/core/src/lib/components/blocks/ModifiedBlock.svelte @@ -9,7 +9,7 @@ export let component: BlockComponent; -
+
{#each lines as line}
diff --git a/packages/core/src/lib/components/blocks/RemovedBlock.svelte b/packages/core/src/lib/components/blocks/RemovedBlock.svelte index 27c1238..52f6434 100644 --- a/packages/core/src/lib/components/blocks/RemovedBlock.svelte +++ b/packages/core/src/lib/components/blocks/RemovedBlock.svelte @@ -8,7 +8,7 @@ export let lines: Line[]; -
+
{#each lines as line}
diff --git a/packages/core/src/lib/components/blocks/RemovedBlockPlaceholder.svelte b/packages/core/src/lib/components/blocks/RemovedBlockPlaceholder.svelte index 1d2cbfd..058ac1d 100644 --- a/packages/core/src/lib/components/blocks/RemovedBlockPlaceholder.svelte +++ b/packages/core/src/lib/components/blocks/RemovedBlockPlaceholder.svelte @@ -6,4 +6,4 @@ export let component: BlockComponent; -
+
diff --git a/packages/core/src/lib/components/blocks/ResolvedConflictBlock.svelte b/packages/core/src/lib/components/blocks/ResolvedConflictBlock.svelte index 823ce13..26682c6 100644 --- a/packages/core/src/lib/components/blocks/ResolvedConflictBlock.svelte +++ b/packages/core/src/lib/components/blocks/ResolvedConflictBlock.svelte @@ -8,7 +8,7 @@ export let component: BlockComponent; -
+
{#each lines as line}
diff --git a/packages/core/src/lib/components/blocks/ResolvedConflictPlaceholder.svelte b/packages/core/src/lib/components/blocks/ResolvedConflictPlaceholder.svelte index efd12c9..a5aba9b 100644 --- a/packages/core/src/lib/components/blocks/ResolvedConflictPlaceholder.svelte +++ b/packages/core/src/lib/components/blocks/ResolvedConflictPlaceholder.svelte @@ -6,4 +6,4 @@ export let component: BlockComponent; -
+
diff --git a/packages/core/src/lib/components/blocks/UnchangedBlock.svelte b/packages/core/src/lib/components/blocks/UnchangedBlock.svelte index 8dd4799..befe33b 100644 --- a/packages/core/src/lib/components/blocks/UnchangedBlock.svelte +++ b/packages/core/src/lib/components/blocks/UnchangedBlock.svelte @@ -6,7 +6,7 @@ export let component: BlockComponent; -
+
{#each lines as line}
diff --git a/packages/core/src/lib/internal/editor/component.ts b/packages/core/src/lib/internal/editor/component.ts index ca0a1a5..bb4298e 100644 --- a/packages/core/src/lib/internal/editor/component.ts +++ b/packages/core/src/lib/internal/editor/component.ts @@ -23,7 +23,7 @@ export class BlockComponent< BlockProps extends Record = Record, SideActionProps extends Record = Record > { - public readonly id = '@' + nanoid(6); + public readonly id = nanoid(8); public readonly blockId: string; public readonly component: typeof SvelteComponent< { component: BlockComponent } & BlockProps diff --git a/packages/core/src/lib/internal/editor/connection.ts b/packages/core/src/lib/internal/editor/connection.ts index 22edf57..e4df38d 100644 --- a/packages/core/src/lib/internal/editor/connection.ts +++ b/packages/core/src/lib/internal/editor/connection.ts @@ -57,12 +57,12 @@ export function drawConnections( const width = canvas.width; - const blocks = Array.from(container.querySelectorAll('.msm__block')); + const blocks = Array.from(container.querySelectorAll('.msm__block')) as HTMLDivElement[]; ctx.reset(); for (const connection of connections) { - const fromElem = blocks.find((elem) => elem.id === connection.from.id); - const toElem = blocks.find((elem) => elem.id === connection.to.id); + const fromElem = blocks.find((elem) => elem.dataset.componentId === connection.from.id); + const toElem = blocks.find((elem) => elem.dataset.componentId === connection.to.id); if ( !fromElem || !toElem || diff --git a/packages/core/src/lib/internal/editor/merging.ts b/packages/core/src/lib/internal/editor/merging.ts index b69f1f0..320cbe6 100644 --- a/packages/core/src/lib/internal/editor/merging.ts +++ b/packages/core/src/lib/internal/editor/merging.ts @@ -17,7 +17,7 @@ export function mergeComponent(data: { components: BlockComponent[]; container: HTMLElement; }) { - const sourceElem = data.container.querySelector(`[id="${data.source.id}"]`); + const sourceElem = data.container.querySelector(`[data-component-id="${data.source.id}"]`); if (!sourceElem) { if (dev) console.error('Failed to merge component: source element not found'); @@ -37,7 +37,9 @@ export function mergeComponent(data: { if (!targetComponent) return; - const targetElem = data.container.querySelector(`[id="${targetComponent.id}"]`); + const targetElem = data.container.querySelector( + `[data-component-id="${targetComponent.id}"]` + ) as HTMLDivElement | null; if (!targetElem) { if (dev) console.error('Failed to merge component: corresponding component element not found'); @@ -63,7 +65,12 @@ export function mergeComponent(data: { let elemFound = false; for (const child of parent.children) { - if (child.id === targetElem.id) { + if (!(child instanceof HTMLDivElement)) { + if (dev) console.error('Failed to merge component: child is not an HTMLDivElement'); + continue; + } + + if (child.dataset.componentId === targetElem.dataset.componentId) { elemFound = true; continue; }