Skip to content

Commit

Permalink
chore: use custom attribute instead of id
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Feb 15, 2024
1 parent 61e323c commit ac68691
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Here is a basic explanation of how the the rendered html looks like:
<!-- Blocks wrapper -->
<div class="msm__wrapper">
<!-- Blocks -->
<div id="@abcdef" class="msm__block block-type">
<div data-component-id="abcdefgh" class="msm__block block-type">
<!-- Lines -->
<div class="msm__line">
<!-- ... -->
Expand Down
2 changes: 1 addition & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Here is a basic explanation of how the the rendered html looks like:
<!-- Blocks wrapper -->
<div class="msm__wrapper">
<!-- Blocks -->
<div id="@abcdef" class="msm__block block-type">
<div data-component-id="abcdefgh" class="msm__block block-type">
<!-- Lines -->
<div class="msm__line">
<!-- ... -->
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/components/blocks/AddedBlock.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let lines: Line[];
</script>

<div class="msm__block {block.type}" id={component.id}>
<div class="msm__block {block.type}" data-component-id={component.id}>
{#each lines as line}
<div class="msm__line">
<div class="msm__content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
export let component: BlockComponent;
</script>

<div class="msm__block {block.placeholderType}" id={component.id} />
<div class="msm__block {block.placeholderType}" data-component-id={component.id} />
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let component: BlockComponent;
</script>

<div class="msm__block {block.type}" id={component.id}>
<div class="msm__block {block.type}" data-component-id={component.id}>
{#each lines as line}
<div class="msm__line">
<div class="msm__content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
export let component: BlockComponent;
</script>

<div class="msm__block {block.placeholderType}" id={component.id} />
<div class="msm__block {block.placeholderType}" data-component-id={component.id} />
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
export let component: BlockComponent;
</script>

<div class="msm__block {block.type}" id={component.id}>
<div class="msm__block {block.type}" data-component-id={component.id}>
{#each lines as line}
<div class="msm__line">
<div class="msm__content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let lines: Line[];
</script>

<div class="msm__block {block.type}" id={component.id}>
<div class="msm__block {block.type}" data-component-id={component.id}>
{#each lines as line}
<div class="msm__line">
<div class="msm__content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
export let component: BlockComponent;
</script>

<div class="msm__block {block.placeholderType}" id={component.id} />
<div class="msm__block {block.placeholderType}" data-component-id={component.id} />
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let component: BlockComponent;
</script>

<div class="msm__block {block.resolvedType}" id={component.id}>
<div class="msm__block {block.resolvedType}" data-component-id={component.id}>
{#each lines as line}
<div class="msm__line">
<div class="msm__content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
export let component: BlockComponent;
</script>

<div class="msm__block {block.resolvedPlaceholderType}" id={component.id} />
<div class="msm__block {block.resolvedPlaceholderType}" data-component-id={component.id} />
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export let component: BlockComponent;
</script>

<div class="msm__block {component.type}" id={component.id}>
<div class="msm__block {component.type}" data-component-id={component.id}>
{#each lines as line}
<div class="msm__line">
<div class="msm__content">
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/internal/editor/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class BlockComponent<
BlockProps extends Record<string, unknown> = Record<string, unknown>,
SideActionProps extends Record<string, unknown> = Record<string, unknown>
> {
public readonly id = '@' + nanoid(6);
public readonly id = nanoid(8);
public readonly blockId: string;
public readonly component: typeof SvelteComponent<
{ component: BlockComponent<BlockProps> } & BlockProps
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/internal/editor/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/lib/internal/editor/merging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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;
}
Expand Down

0 comments on commit ac68691

Please sign in to comment.