Skip to content

Commit

Permalink
fix: should allow different HTMLs sharing the same Id #1475
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoiver committed Aug 11, 2023
1 parent e971431 commit c99c632
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
35 changes: 35 additions & 0 deletions __tests__/unit/display-objects/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,39 @@ describe('HTML', () => {
expect($el.style.fontSize).toBe('16px');
expect($el.style.color).toBe('blue');
});

it('should allow different HTMLs sharing the same Id.', async () => {
const html1 = new HTML({
id: 'id',
style: {
x: 100,
y: 100,
width: 100,
height: 100,
innerHTML: '<h1>This is Title</h1>',
},
});
const html2 = new HTML({
id: 'id',
style: {
x: 100,
y: 100,
width: 100,
height: 100,
innerHTML: '<h1>This is Title</h1>',
},
});

await canvas.ready;
canvas.appendChild(html1);
canvas.appendChild(html2);

const $el1 = html1.getDomElement();
const $el2 = html2.getDomElement();
expect($el1.id).toBe('id');
expect($el2.id).toBe('id');
// unique id
expect($el1.dataset.id).toBe('g-html-' + html1.entity);
expect($el2.dataset.id).toBe('g-html-' + html2.entity);
});
});
18 changes: 4 additions & 14 deletions packages/g-plugin-html-renderer/src/HTMLRenderingPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ export class HTMLRenderingPlugin implements RenderingPlugin {
$el.remove();
this.context.nativeHTMLMap.delete($el);
}

// const existedId = this.getId(object);
// const $existedElement: HTMLElement | null = this.$camera.querySelector('#' + existedId);
// if ($existedElement) {
// this.$camera.removeChild($existedElement);
// }
}
};

Expand Down Expand Up @@ -166,10 +160,6 @@ export class HTMLRenderingPlugin implements RenderingPlugin {
});
}

private getId(object: DisplayObject) {
return object.id || HTML_PREFIX + object.entity;
}

private createCamera(camera: ICamera) {
const { document: doc, width, height } = this.context.config;
const $canvas =
Expand Down Expand Up @@ -209,15 +199,15 @@ export class HTMLRenderingPlugin implements RenderingPlugin {

private getOrCreateEl(object: DisplayObject) {
const { document: doc } = this.context.config;
const existedId = this.getId(object);

const uniqueHTMLId = `${HTML_PREFIX}${object.entity}`;
let $existedElement: HTMLElement | null = this.$camera.querySelector(
'#' + existedId,
`[data-id=${uniqueHTMLId}]`,
);
if (!$existedElement) {
$existedElement = (doc || document).createElement('div');
object.parsedStyle.$el = $existedElement;
$existedElement.id = existedId;
$existedElement.id = object.id || uniqueHTMLId;
$existedElement.dataset.id = uniqueHTMLId;

if (object.name) {
$existedElement.setAttribute('name', object.name);
Expand Down

0 comments on commit c99c632

Please sign in to comment.