Skip to content

Commit

Permalink
Require a component class when creating an EmberNodeView
Browse files Browse the repository at this point in the history
This replaces the `componentPath` with a `component` argument. Users need to provide the component class instead of the path which makes the setup Embroider compatible.
  • Loading branch information
Windvis committed Sep 6, 2023
1 parent 67d15af commit e2324a3
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 20 deletions.
25 changes: 25 additions & 0 deletions .changeset/old-avocados-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
'@lblod/ember-rdfa-editor': major
---

Add Embroider "optimized" support

To support the strict Embroider "optimized" preset we needed to make a breaking change. The `componentPath` property for the `createEmberNodeView` util has been replaced by a new `component` property. Instead of providing the path to the component, the component class should be passed instead.

Before:
```js
createEmberNodeView({
// ... other options
componentPath: 'foo',
});
```

After:
```js
import Foo from 'app-name/components/foo';

createEmberNodeView({
// ... other options
component: Foo,
});
```
4 changes: 3 additions & 1 deletion addon/plugins/image/nodes/image.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { ComponentLike } from '@glint/template';
import {
createEmberNodeSpec,
createEmberNodeView,
EmberNodeConfig,
} from '@lblod/ember-rdfa-editor/utils/ember-node';
import Image from '@lblod/ember-rdfa-editor/components/plugins/image/node';
import { Node as PNode } from 'prosemirror-model';

const emberNodeConfig: EmberNodeConfig = {
name: 'image',
componentPath: 'plugins/image/node',
component: Image as unknown as ComponentLike,
inline: true,
group: 'inline',
draggable: true,
Expand Down
4 changes: 3 additions & 1 deletion addon/plugins/link/nodes/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
createEmberNodeView,
EmberNodeConfig,
} from '../../../utils/ember-node';
import type { ComponentLike } from '@glint/template';
import Link from '@lblod/ember-rdfa-editor/components/ember-node/link';

type LinkOptions = {
interactive: boolean;
Expand All @@ -15,7 +17,7 @@ const emberNodeConfig: (options: LinkOptions) => EmberNodeConfig = (
const { interactive } = options;
return {
name: 'link',
componentPath: 'ember-node/link',
component: Link as unknown as ComponentLike,
inline: true,
group: 'inline',
content: 'text*',
Expand Down
32 changes: 17 additions & 15 deletions addon/utils/_private/ember-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { v4 as uuidv4 } from 'uuid';
// eslint-disable-next-line ember/no-classic-components
import Component from '@ember/component';
import Owner from '@ember/owner';
import type { ComponentLike } from '@glint/template';
import SayController from '@lblod/ember-rdfa-editor/core/say-controller';
import { EditorState, SayView } from '@lblod/ember-rdfa-editor';
import SayNodeSpec from '@lblod/ember-rdfa-editor/core/say-node-spec';
Expand Down Expand Up @@ -105,7 +106,7 @@ export function emberComponent(
template: TemplateFactory,
props: EmberNodeArgs & {
atom: boolean;
componentPath: string;
component: ComponentLike<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
contentDOM?: HTMLElement;
},
): { node: HTMLElement; component: EmberInlineComponent } {
Expand Down Expand Up @@ -147,20 +148,21 @@ class EmberNodeView implements NodeView {
// when a node gets updated, `update()` is called.
// We set the new node here and pass it to the component to render it.
this.config = emberNodeConfig;
const { name, componentPath, atom, inline } = emberNodeConfig;
this.template = hbs`{{#component this.componentPath
getPos=this.getPos
node=this.node
updateAttribute=this.updateAttribute
controller=this.controller
view=this.view
selected=this.selected
contentDecorations=this.contentDecorations
}}
const { name, component: componentClass, atom, inline } = emberNodeConfig;

this.template = hbs`<this.component
@getPos={{this.getPos}}
@node={{this.node}}
@updateAttribute={{this.updateAttribute}}
@controller={{this.controller}}
@view={{this.view}}
@selected={{this.selected}}
@contentDecorations={{this.contentDecorations}}
>
{{#unless this.atom}}
<EmberNode::Slot @contentDOM={{this.contentDOM}}/>
<EmberNode::Slot @contentDOM={{this.contentDOM}}/>
{{/unless}}
{{/component}}`;
</this.component>`;
this.node = pNode;
this.contentDOM = !atom
? document.createElement(inline ? 'span' : 'div', {})
Expand All @@ -187,7 +189,7 @@ class EmberNodeView implements NodeView {
},
controller,
contentDOM: this.contentDOM,
componentPath,
component: componentClass,
atom,
view,
selected: false,
Expand Down Expand Up @@ -292,7 +294,7 @@ class EmberNodeView implements NodeView {

export type EmberNodeConfig = {
name: string;
componentPath: string;
component: ComponentLike<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
inline: boolean;
group: string;
content?: string;
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
},
"peerDependencies": {
"@appuniversum/ember-appuniversum": "^2.4.2",
"@glint/template": "^1.1.0",
"ember-cli-sass": "^11.0.1",
"ember-modifier": "^3.2.7"
},
Expand Down
11 changes: 11 additions & 0 deletions tests/dummy/app/components/sample-ember-nodes/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import templateOnlyComponent from '@ember/component/template-only';

interface Signature {
Blocks: {
default: [];
};
}

const Card = templateOnlyComponent<Signature>();

export default Card;
4 changes: 3 additions & 1 deletion tests/dummy/app/dummy-nodes/card.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { ComponentLike } from '@glint/template';
import {
createEmberNodeSpec,
createEmberNodeView,
EmberNodeConfig,
} from '@lblod/ember-rdfa-editor/utils/_private/ember-node';
import Card from 'dummy/components/sample-ember-nodes/card';

const emberNodeConfig: EmberNodeConfig = {
name: 'card',
componentPath: 'sample-ember-nodes/card',
component: Card as unknown as ComponentLike,
inline: false,
group: 'block',
content: 'block+',
Expand Down
4 changes: 3 additions & 1 deletion tests/dummy/app/dummy-nodes/counter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ComponentLike } from '@glint/template';
import { emberApplicationPluginKey } from '@lblod/ember-rdfa-editor/plugins/ember-application';
import {
createEmberNodeSpec,
Expand All @@ -6,10 +7,11 @@ import {
} from '@lblod/ember-rdfa-editor/utils/_private/ember-node';
import { optionMapOr } from '@lblod/ember-rdfa-editor/utils/_private/option';
import IntlService from 'ember-intl/services/intl';
import Counter from 'dummy/components/sample-ember-nodes/counter';

const emberNodeConfig: EmberNodeConfig = {
name: 'counter',
componentPath: 'sample-ember-nodes/counter',
component: Counter as unknown as ComponentLike,
inline: true,
group: 'inline',
atom: true,
Expand Down
4 changes: 3 additions & 1 deletion tests/dummy/app/dummy-nodes/dropdown.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { ComponentLike } from '@glint/template';
import {
createEmberNodeSpec,
createEmberNodeView,
EmberNodeConfig,
} from '@lblod/ember-rdfa-editor/utils/_private/ember-node';
import Dropdown from 'dummy/components/sample-ember-nodes/dropdown';

const emberNodeConfig: EmberNodeConfig = {
name: 'dropdown',
componentPath: 'sample-ember-nodes/dropdown',
component: Dropdown as unknown as ComponentLike,
inline: true,
group: 'inline',
atom: true,
Expand Down

0 comments on commit e2324a3

Please sign in to comment.