Skip to content

Allow for custom plugins to be passed in to the text editor and initialised when editor is created #3124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Plugin, PluginKey } from 'prosemirror-state';

export class CustomEditorPlugin<T = any> {
private key: PluginKey;

constructor(
private props: T,
private name: string,
) {
this.key = new PluginKey(name);
}

public getPluginKey(): PluginKey {
return this.key;
}

public getName(): string {
return this.name;
}

public getProps(): T {
return this.props;
}

public createPlugin(): Plugin {
return new Plugin({
key: this.getPluginKey(),
props: this.getProps(),
});
}
}

export const pluginFactory = (plugin: CustomEditorPlugin): Plugin => {
return plugin.createPlugin();
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { EditorState, Transaction } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
import { Schema, DOMParser } from 'prosemirror-model';
import { Plugin } from 'prosemirror-state';

Check failure on line 14 in src/components/text-editor/prosemirror-adapter/prosemirror-adapter.tsx

View workflow job for this annotation

GitHub Actions / Lint

'prosemirror-state' import is duplicated
import { schema } from 'prosemirror-schema-basic';
import { addListNodes } from 'prosemirror-schema-list';
import { exampleSetup } from 'prosemirror-example-setup';
Expand Down Expand Up @@ -76,6 +77,12 @@
@Prop({ reflect: true })
public language: Languages;

/**
* An array of custom ProseMirror plugins to load into the editor.
*/
@Prop()
public customPlugins: Array<Plugin> = [];

Check failure on line 84 in src/components/text-editor/prosemirror-adapter/prosemirror-adapter.tsx

View workflow job for this annotation

GitHub Actions / Lint

Array type using 'Array<Plugin>' is forbidden for simple types. Use 'Plugin[]' instead

@Element()
private host: HTMLLimelTextEditorElement;

Expand Down Expand Up @@ -288,6 +295,7 @@
this.updateActiveActionBarItems,
),
createActionBarInteractionPlugin(this.menuCommandFactory),
...this.customPlugins,
],
});
}
Expand Down
27 changes: 27 additions & 0 deletions src/components/text-editor/text-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Component, Event, EventEmitter, Host, Prop, h } from '@stencil/core';
import { Plugin } from 'prosemirror-state';
import { FormComponent } from '../form/form.types';
import { Languages } from '../date-picker/date.types';
import { createRandomString } from '../../util/random-string';
import {
CustomEditorPlugin,
pluginFactory,
} from './prosemirror-adapter/plugins/plugin-factory';

/**
* A rich text editor that offers a rich text editing experience with markdown support,
* in the sense that you can easily type markdown syntax and see the rendered
Expand Down Expand Up @@ -120,6 +126,9 @@ export class TextEditor implements FormComponent<string> {
private helperTextId: string;
private editorId: string;

@Prop()
public customPlugins: CustomEditorPlugin[] = [];

public constructor() {
this.helperTextId = createRandomString();
this.editorId = createRandomString();
Expand Down Expand Up @@ -173,12 +182,30 @@ export class TextEditor implements FormComponent<string> {
tabindex={this.disabled ? -1 : 0}
aria-disabled={this.disabled}
language={this.language}
customPlugins={this.createPlugins(this.customPlugins)}
/>,
this.renderPlaceholder(),
this.renderHelperLine(),
];
}

private createPlugins(plugins: CustomEditorPlugin[]): Plugin[] {
const pluginArray: Plugin[] = [];

for (const plugin of plugins) {
if (!plugin) {
return;
}

const createdPlugin = pluginFactory(plugin);
if (createdPlugin) {
pluginArray.push(createdPlugin);
}
}

return pluginArray;
}

private renderLabel() {
if (!this.label) {
return;
Expand Down
Loading