Add ActionInvoker

This commit is contained in:
Stanislav Romanov 2022-09-15 13:30:58 +03:00
parent 7dfbb56d1f
commit 44967b128c
5 changed files with 53 additions and 18 deletions

View file

@ -1,18 +1,20 @@
<Form::Element @label={{namelize @name}} @errors={{array this.error}}>
<MonacoEditor
@value={{json-stringify this.value}}
@language='json'
@sendValue={{this.save}}
class='h-40'
as |monaco|
>
<Button
@text='Save'
@style='primary'
@size='sm'
@onClick={{monaco.invokeSendValue}}
<div class='mb-4'>
<Form::Element @label={{namelize @name}} @errors={{array this.error}}>
<MonacoEditor
@value={{json-stringify this.value}}
@language='json'
@sendValue={{this.save}}
@invoker={{this.invoker}}
class='h-40'
/>
<Button @text='Cancel' @size='sm' @onClick={{this.setInitialValue}} />
<Button @text='Reset' @size='sm' @onClick={{this.reset}} />
</MonacoEditor>
</Form::Element>
</Form::Element>
<Button
@text='Save'
@style='primary'
@size='sm'
@onClick={{fn this.invoker.invoke 'invokeSendValue'}}
/>
<Button @text='Cancel' @size='sm' @onClick={{this.setInitialValue}} />
<Button @text='Reset' @size='sm' @onClick={{this.reset}} />
</div>

View file

@ -1,6 +1,7 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import ActionInvoker from '../lib/action-invoker';
export default class FormIndexSettingComponent extends Component {
@tracked value;
@ -9,6 +10,8 @@ export default class FormIndexSettingComponent extends Component {
constructor() {
super(...arguments);
this.invoker = new ActionInvoker();
this.setInitialValue();
}

View file

@ -1,7 +1,7 @@
<div
{{did-insert this.initEditor}}
{{did-update this.updateValue @value}}
class='border mb-4'
class='border'
...attributes
></div>

View file

@ -3,6 +3,12 @@ import Component from '@glimmer/component';
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
export default class MonacoEditorComponent extends Component {
constructor(owner, args) {
super(owner, args);
this.args.invoker?.subscribe(this);
}
@action
initEditor(el) {
this.editor = monaco.editor.create(el, {

24
app/lib/action-invoker.js Normal file
View file

@ -0,0 +1,24 @@
import { registerDestructor } from '@ember/destroyable';
import { action } from '@ember/object';
export default class ActionInvoker {
constructor() {
this.contexts = new Set();
}
@action
invoke(action, ...args) {
this.contexts.forEach((item) => {
item[action]?.(...args);
});
}
@action
subscribe(context) {
this.contexts = this.contexts.add(context);
registerDestructor(context, () => {
this.contexts.delete(context);
});
}
}