mirror of https://github.com/artf/grapesjs.git
4 changed files with 167 additions and 123 deletions
@ -1,111 +1,158 @@ |
|||||
import Editor from '../../../src/editor'; |
import Editor from '../../../src/editor'; |
||||
import EditorModel from '../../../src/editor/model/Editor'; |
import EditorModel from '../../../src/editor/model/Editor'; |
||||
import ComponentWrapper from '../../../src/dom_components/model/ComponentWrapper'; |
import ComponentWrapper from '../../../src/dom_components/model/ComponentWrapper'; |
||||
|
import { setupTestEditor } from '../../common'; |
||||
|
|
||||
describe('Pages with same component ids across pages', () => { |
describe('Pages with same component ids across pages', () => { |
||||
let editor: Editor; |
let editor: Editor; |
||||
let em: EditorModel; |
let em: EditorModel; |
||||
let pm: Editor['Pages']; |
let pm: Editor['Pages']; |
||||
let domc: Editor['Components']; |
let domc: Editor['Components']; |
||||
const getTitle = (wrapper: ComponentWrapper) => wrapper.components().at(0)!.components().at(0)!; |
const rootDefaultProps = { |
||||
|
type: 'wrapper', |
||||
beforeAll(() => { |
head: { type: 'head' }, |
||||
editor = new Editor({ |
docEl: { tagName: 'html' }, |
||||
pageManager: { |
stylable: [ |
||||
pages: [ |
'background', |
||||
{ |
'background-color', |
||||
id: 'p1', |
'background-image', |
||||
frames: [ |
'background-repeat', |
||||
{ |
'background-attachment', |
||||
component: { |
'background-position', |
||||
type: 'wrapper', |
'background-size', |
||||
attributes: { id: 'body' }, |
], |
||||
components: [ |
}; |
||||
{ |
|
||||
tagName: 'section', |
const getTitle = (wrapper: ComponentWrapper) => wrapper.components().at(0); |
||||
components: [ |
|
||||
{ |
const getRootComponent = ({ idBody = 'body', idTitle = 'main-title', contentTitle = 'A' } = {}) => ({ |
||||
tagName: 'h1', |
type: 'wrapper', |
||||
type: 'text', |
attributes: { id: idBody }, |
||||
attributes: { id: 'main-title' }, |
components: [ |
||||
components: [{ type: 'textnode', content: 'A' }], |
{ |
||||
}, |
tagName: 'h1', |
||||
], |
type: 'text', |
||||
}, |
attributes: { id: idTitle }, |
||||
], |
components: [{ type: 'textnode', content: contentTitle }], |
||||
}, |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
{ |
|
||||
id: 'p2', |
|
||||
frames: [ |
|
||||
{ |
|
||||
component: { |
|
||||
type: 'wrapper', |
|
||||
attributes: { id: 'body' }, |
|
||||
components: [ |
|
||||
{ |
|
||||
tagName: 'section', |
|
||||
components: [ |
|
||||
{ |
|
||||
tagName: 'h1', |
|
||||
type: 'text', |
|
||||
attributes: { id: 'main-title' }, |
|
||||
components: [{ type: 'textnode', content: 'B' }], |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
], |
|
||||
}, |
}, |
||||
}); |
], |
||||
|
}); |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
({ editor } = setupTestEditor()); |
||||
em = editor.getModel(); |
em = editor.getModel(); |
||||
pm = em.Pages; |
pm = em.Pages; |
||||
domc = em.Components; |
domc = em.Components; |
||||
pm.onLoad(); |
|
||||
}); |
}); |
||||
|
|
||||
afterAll(() => { |
afterEach(() => { |
||||
editor.destroy(); |
editor.destroy(); |
||||
}); |
}); |
||||
|
|
||||
test('Handles pages with components having the same id across pages', () => { |
test('Default behavior with pages having components with same ids are incremented', () => { |
||||
const pages = pm.getAll(); |
editor.Pages.add({ |
||||
expect(pages.length).toBe(2); |
id: 'page1', |
||||
|
frames: [{ component: getRootComponent() }], |
||||
|
}); |
||||
|
editor.Pages.add({ |
||||
|
id: 'page2', |
||||
|
frames: [{ component: getRootComponent({ contentTitle: 'B' }) }], |
||||
|
}); |
||||
|
|
||||
const p1 = pages[0]; |
const root1 = pm.get('page1')!.getMainComponent(); |
||||
const p2 = pages[1]; |
const root2 = pm.get('page2')!.getMainComponent(); |
||||
|
|
||||
const w1 = p1.getMainComponent(); |
expect(editor.getHtml({ component: root1 })).toBe('<body id="body"><h1 id="main-title">A</h1></body>'); |
||||
const w2 = p2.getMainComponent(); |
expect(editor.getHtml({ component: root2 })).toBe('<body id="body-2"><h1 id="main-title-2">B</h1></body>'); |
||||
|
|
||||
expect(w1.getId()).toBe('body'); |
expect(JSON.parse(JSON.stringify(root1))).toEqual({ |
||||
expect(w2.getId()).toBe('body'); |
...rootDefaultProps, |
||||
|
attributes: { id: 'body' }, |
||||
|
components: [ |
||||
|
{ |
||||
|
tagName: 'h1', |
||||
|
type: 'text', |
||||
|
attributes: { id: 'main-title' }, |
||||
|
components: [{ type: 'textnode', content: 'A' }], |
||||
|
}, |
||||
|
], |
||||
|
}); |
||||
|
|
||||
const t1 = getTitle(w1); |
expect(JSON.parse(JSON.stringify(root2))).toEqual({ |
||||
const t2 = getTitle(w2); |
...rootDefaultProps, |
||||
|
attributes: { id: 'body-2' }, |
||||
|
components: [ |
||||
|
{ |
||||
|
tagName: 'h1', |
||||
|
type: 'text', |
||||
|
attributes: { id: 'main-title-2' }, |
||||
|
components: [{ type: 'textnode', content: 'B' }], |
||||
|
}, |
||||
|
], |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
test('Handles pages with components having the same id across pages', () => { |
||||
|
editor.Components.config.keepAttributeIdsCrossPages = true; |
||||
|
editor.Pages.add({ |
||||
|
id: 'page1', |
||||
|
frames: [{ component: getRootComponent() }], |
||||
|
}); |
||||
|
editor.Pages.add({ |
||||
|
id: 'page2', |
||||
|
frames: [{ component: getRootComponent({ contentTitle: 'B' }) }], |
||||
|
}); |
||||
|
const page1 = pm.get('page1')!; |
||||
|
const page2 = pm.get('page2')!; |
||||
|
const root1 = page1.getMainComponent(); |
||||
|
const root2 = page2.getMainComponent(); |
||||
|
|
||||
|
expect(root1.getId()).toBe('body'); |
||||
|
expect(root2.getId()).toBe('body'); |
||||
|
|
||||
|
const title1 = getTitle(root1); |
||||
|
const title2 = getTitle(root2); |
||||
|
|
||||
// IDs should be preserved per page but stored uniquely in the shared map
|
// IDs should be preserved per page but stored uniquely in the shared map
|
||||
expect(t1.getId()).toBe('main-title'); |
expect(title1.getId()).toBe('main-title'); |
||||
expect(t2.getId()).toBe('main-title'); |
expect(title2.getId()).toBe('main-title'); |
||||
|
|
||||
const all = domc.allById(); |
const all = domc.allById(); |
||||
|
|
||||
expect(all['body']).toBe(w1); |
expect(all['body']).toBe(root1); |
||||
expect(all['body-2']).toBe(w2); |
expect(all['body-2']).toBe(root2); |
||||
expect(all['main-title']).toBe(t1); |
expect(all['main-title']).toBe(title1); |
||||
expect(all['main-title-2']).toBe(t2); |
expect(all['main-title-2']).toBe(title2); |
||||
|
|
||||
|
expect(editor.getHtml({ component: root1 })).toBe('<body id="body"><h1 id="main-title">A</h1></body>'); |
||||
|
expect(editor.getHtml({ component: root2 })).toBe('<body id="body"><h1 id="main-title">B</h1></body>'); |
||||
|
|
||||
const html1 = editor.getHtml({ component: w1 }); |
expect(JSON.parse(JSON.stringify(root1))).toEqual({ |
||||
const html2 = editor.getHtml({ component: w2 }); |
...rootDefaultProps, |
||||
|
attributes: { id: 'body' }, |
||||
|
components: [ |
||||
|
{ |
||||
|
tagName: 'h1', |
||||
|
type: 'text', |
||||
|
attributes: { id: 'main-title' }, |
||||
|
components: [{ type: 'textnode', content: 'A' }], |
||||
|
}, |
||||
|
], |
||||
|
}); |
||||
|
|
||||
expect(html1).toContain('A'); |
expect(JSON.parse(JSON.stringify(root2))).toEqual({ |
||||
expect(html2).toContain('B'); |
...rootDefaultProps, |
||||
|
id: 'body-2', |
||||
|
attributes: { id: 'body' }, |
||||
|
components: [ |
||||
|
{ |
||||
|
id: 'main-title-2', |
||||
|
tagName: 'h1', |
||||
|
type: 'text', |
||||
|
attributes: { id: 'main-title' }, |
||||
|
components: [{ type: 'textnode', content: 'B' }], |
||||
|
}, |
||||
|
], |
||||
|
}); |
||||
}); |
}); |
||||
}); |
}); |
||||
|
|||||
Loading…
Reference in new issue