From f9378b9b38befd25663625303b1940cfa7f4612a Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 9 Jun 2023 16:07:09 +0400 Subject: [PATCH] Update components/traits --- src/components.ts | 25 ++++++++++--------------- src/traits.ts | 7 ++++++- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/components.ts b/src/components.ts index c3ab47f..41f5a72 100644 --- a/src/components.ts +++ b/src/components.ts @@ -64,8 +64,16 @@ export default (editor: Editor, opts: RequiredPluginOptions) => { traits, 'script-props': [...typedPropsKeys, 'typedsrc'], script(props: typeof typedProps & { typedsrc: string }) { - console.log({props}); - const strings = typeof props.strings === 'string' ? JSON.parse(props.strings) : props.strings; + const getStrings = (value: string | string[]) => { + if (Array.isArray(value)) { + return value; + } else if (value.indexOf('\n') >= 0) { + return value.split('\n'); + } else { + return [] + } + } + const strings = getStrings(props.strings); const int = (num: any) => parseInt(num, 10) || 0; const bool = (val: any) => !!val; const init = () => { @@ -110,19 +118,6 @@ export default (editor: Editor, opts: RequiredPluginOptions) => { } }, }) as any, - - init() { - const events = traits.filter(i => ['strings'].indexOf(i.name) < 0) - .map(i => `change:${i.name}`).join(' '); - this.on(events, () => this.trigger('change:script')); - this.on('change:strings', this.onStringsChange); - }, - - onStringsChange(_: any, value: any) { - if (Array.isArray(value)) return; - this.set({ strings: value.split('\n') }); - this.trigger('change:script'); - } }, }); }; diff --git a/src/traits.ts b/src/traits.ts index 1344a94..f556497 100644 --- a/src/traits.ts +++ b/src/traits.ts @@ -9,6 +9,11 @@ export default (editor: Editor) => { onUpdate({ component, elInput }) { elInput.value = component.get('strings').join('\n'); - } + }, + + onEvent({ component, elInput }) { + const value = (elInput.value || '').split('\n'); + component.set('strings', value); + }, }); } \ No newline at end of file