diff --git a/apps/table-plugin/src/app/app.component.html b/apps/table-plugin/src/app/app.component.html index da06b50..1d78bd3 100644 --- a/apps/table-plugin/src/app/app.component.html +++ b/apps/table-plugin/src/app/app.component.html @@ -1,4 +1,4 @@ -
+

Import a data file (CSV)

>(window, 'message'); @@ -51,6 +59,10 @@ export class AppComponent { ) ); + constructor() { + this.initConfig(); + } + onSelectFile(event: Event) { const target = event.target as HTMLInputElement; if ( @@ -71,7 +83,7 @@ export class AppComponent { content: { import: this.table, type: 'import', - options: this.tableOptions, + options: this.form.value, }, type: 'table', }); @@ -87,7 +99,7 @@ export class AppComponent { content: { new: { column: data.column, row: data.row }, type: 'new', - options: this.tableOptions, + options: this.form.value, }, type: 'table', }); @@ -117,6 +129,54 @@ export class AppComponent { }; } + private initConfig(): void { + this.messages$ + .pipe( + filter( + (event) => + event.data.type === 'tableconfig' && + event.data.content.type === 'retrieve' + ), + take(1), + map((event) => { + const data = (event.data as TableConfigEvent).content.options; + + return data; + }) + ) + .subscribe((data) => { + if (data) { + this.form.patchValue(data, { emitEvent: false }); + } else { + this.form.patchValue({ + filledHeaderRow: true, + filledHeaderColumn: false, + borders: true, + alternateRows: true, + }); + } + }); + + this.form.valueChanges + .pipe(takeUntilDestroyed()) + .subscribe((options: TableOptions) => { + this.sendMessage({ + type: 'tableconfig', + content: { + type: 'save', + options, + }, + }); + }); + + this.sendMessage({ + type: 'tableconfig', + content: { + type: 'retrieve', + }, + }); + } + private sendMessage(message: PluginMessageEvent): void { parent.postMessage(message, '*'); } diff --git a/apps/table-plugin/src/app/model.ts b/apps/table-plugin/src/app/model.ts index 70efa6b..13e7653 100644 --- a/apps/table-plugin/src/app/model.ts +++ b/apps/table-plugin/src/app/model.ts @@ -5,6 +5,14 @@ export interface InitPluginEvent { }; } +export interface TableConfigEvent { + type: 'tableconfig'; + content: { + type: 'save' | 'retrieve'; + options?: TableOptions; + }; +} + export interface TablePluginEvent { type: 'table'; content: { @@ -23,7 +31,8 @@ export interface ThemePluginEvent { export type PluginMessageEvent = | InitPluginEvent | TablePluginEvent - | ThemePluginEvent; + | ThemePluginEvent + | TableConfigEvent; export type Cell = { column: number; row: number }; diff --git a/apps/table-plugin/src/plugin.ts b/apps/table-plugin/src/plugin.ts index 2a32e2d..4a02b75 100644 --- a/apps/table-plugin/src/plugin.ts +++ b/apps/table-plugin/src/plugin.ts @@ -6,11 +6,9 @@ penpot.ui.open('TABLE PLUGIN', `?theme=${penpot.getTheme()}`, { height: 610, }); -penpot.on('themechange', (theme) => { - penpot.ui.sendMessage({ type: 'theme', content: theme }); -}); - penpot.ui.onMessage((message) => { + pluginData(message); + if (message.type === 'table') { let numRows = 0; let numCols = 0; @@ -217,3 +215,29 @@ function createFlexCell( } } } + +function pluginData(message: PluginMessageEvent) { + if (message.type === 'tableconfig') { + const { type, options } = message.content; + const page = penpot.getPage(); + + if (type === 'save') { + page?.setPluginData('table-plugin', JSON.stringify(options)); + } else if (message.content.type === 'retrieve') { + const data = page?.getPluginData('table-plugin'); + const options = data ? JSON.parse(data) : null; + + sendMessage({ + type: 'tableconfig', + content: { + type: 'retrieve', + options, + }, + }); + } + } +} + +function sendMessage(message: PluginMessageEvent) { + penpot.ui.sendMessage(message); +}