svg-layout-designer-react/public/smartcomponent/svg-layout-designer.ts
Eric Nguyen 23ed3ed1ad Merged PR 189: Simplify usage of SmartComponent
- Added new Events :
  - AddContainer, AddContainerToSelectedContainer, AppendContainer, AppendContainerToSelectedContainer, SelectContainer, DeleteContainer
  - AddSymbol, SelectSymbol, DeleteSymbol
- Changed the component to an iframe (you only need to copy the whole dist now)
- Added callbacks to every methods in the component
- Create event listener on demand: no need to initialize the event listener!
- Update d.ts
- Added Fastboot and enable it by default on production build
2022-09-21 09:24:14 +00:00

108 lines
4.3 KiB
TypeScript

namespace SmartBusiness.Web.Components {
import IHistoryState = SVGLD.IHistoryState;
import IEditorState = SVGLD.IEditorState;
export class SVGLayoutDesigner extends Components.ComponentBase {
public constructor(componentInfo: KnockoutComponentTypes.ComponentInfo, params: any) {
super(componentInfo, params);
}
public GetEditorComponent() {
const component = this.$component[0]
.querySelector('iframe')
.contentDocument
.querySelector('.Editor');
if (component === undefined) {
throw new Error('[SVGLD] Cannot hook the event because the editor is not yet open')
}
return component;
}
public GetRootComponent() {
return this.$component[0]
.querySelector('iframe')
.contentDocument;
}
public GetCurrentHistoryState(callback: (state: IHistoryState) => void) {
const component = this.GetEditorComponent();
const eventType = 'getCurrentHistoryState';
component.dispatchEvent(new CustomEvent(eventType));
this.AddEventListener(callback, eventType);
}
public GetEditorState(callback: (state: IEditorState) => void) {
const component = this.GetEditorComponent();
const eventType = 'getEditorState';
component.dispatchEvent(new CustomEvent(eventType));
this.AddEventListener(callback, eventType);
}
public AppendNewHistoryState(historyState: SVGLD.IHistoryState, callback?: (state: IEditorState) => void) {
const eventType = 'appendNewState';
this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail: historyState }));
this.AddEventListener(callback, eventType);
}
public AddContainer(index: number, type: string, parentId: string, callback?: (state: IEditorState) => void) {
const detail = {
index,
type,
parentId
}
const eventType = 'addContainer';
this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail }));
this.AddEventListener(callback, eventType);
}
public AddContainerToSelectedContainer(index: number, type: string, callback?: (state: IEditorState) => void) {
const detail = {
index,
type
}
const eventType = 'addContainerToSelectedContainer';
this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail }));
this.AddEventListener(callback, eventType);
}
public AppendContainer(type: string, parentId: string, callback?: (state: IEditorState) => void) {
const detail = {
type,
parentId
}
const eventType = 'appendContainer';
this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail }));
this.AddEventListener(callback, eventType);
}
public AppendContainerToSelectedContainer(type: string, callback?: (state: IEditorState) => void) {
const detail = {
type
}
const eventType = 'appendContainerToSelectedContainer';
this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail }));
this.AddEventListener(callback, eventType);
}
private AddEventListener(callback: (...args: any[]) => void, eventType: string) {
const root = this.GetRootComponent();
const listener = (e: CustomEvent) => {
e.target.removeEventListener(e.type, listener);
callback && callback(e.detail);
};
root.addEventListener(eventType, listener);
}
}
ko.components.register('svg-layout-designer', {
viewModel: {
createViewModel: function (params, componentInfo) {
return new SmartBusiness.Web.Components.SVGLayoutDesigner(componentInfo, params);
}
},
template: { element: 'svg-layout-designer' }
});
}