Skip to content

Commit

Permalink
feat(text editor): use new popup trigger plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
john-traas committed Aug 26, 2024
1 parent fc688e3 commit f20bfee
Showing 1 changed file with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { addListNodes } from 'prosemirror-schema-list';
import { exampleSetup } from 'prosemirror-example-setup';
import { keymap } from 'prosemirror-keymap';
import { ActionBarItem } from 'src/components/action-bar/action-bar.types';
import { ListSeparator } from 'src/components/list/list-item.types';
import { ListItem, ListSeparator } from 'src/components/list/list-item.types';
import { MenuCommandFactory } from './menu/menu-commands';
import { menuTranslationIDs, getTextEditorMenuItems } from './menu/menu-items';
import { ContentTypeConverter } from '../utils/content-type-converter';
Expand All @@ -40,6 +40,9 @@ import {
import { createImageRemoverPlugin } from './plugins/image-remover-plugin';
import { createMenuStateTrackingPlugin } from './plugins/menu-state-tracking-plugin';
import { createActionBarInteractionPlugin } from './plugins/menu-action-interaction-plugin';
import { createTriggerPlugin } from './plugins/trigger-popup-plugin';
import { LimelPickerCustomEvent } from '../../../../src/index';
import { mentionTagMark } from './mentions/mark-schema-extender';

/**
* The ProseMirror adapter offers a rich text editing experience with markdown support.
Expand Down Expand Up @@ -98,6 +101,31 @@ export class ProsemirrorAdapter {
@State()
public isLinkMenuOpen: boolean = false;

/**
* Open state of the picker
*/
@State()
public isPickerOpen: boolean = false;

@State()
private selectedItem: ListItem<number>;

private fakeMentionItems: Array<ListItem<number>> = [
{ text: 'Admiral Swiggins', value: 1 },
{ text: 'Ayla', value: 2 },
{ text: 'Clunk', value: 3 },
{ text: 'Coco', value: 4 },
{ text: 'Derpl', value: 5 },
{ text: 'Froggy G', value: 6 },
{ text: 'Gnaw', value: 7 },
{ text: 'Lonestar', value: 8 },
{ text: 'Leon', value: 9 },
{ text: 'Raelynn', value: 10 },
{ text: 'Skølldir', value: 11 },
{ text: 'Voltar', value: 12 },
{ text: 'Yuri', value: 13 },
];

private menuCommandFactory: MenuCommandFactory;
private schema: Schema;
private contentConverter: ContentTypeConverter;
Expand Down Expand Up @@ -153,13 +181,18 @@ export class ProsemirrorAdapter {
'open-editor-link-menu',
this.handleOpenLinkMenu,
);

this.host.addEventListener('open-picker', this.handleOpenPicker);
this.host.addEventListener('close-picker', this.handleClosePicker);
}

public disconnectedCallback() {
this.host.removeEventListener(
'open-editor-link-menu',
this.handleOpenLinkMenu,
);
this.host.removeEventListener('open-picker', this.handleOpenPicker);
this.host.removeEventListener('close-picker', this.handleClosePicker);
this.view.destroy();
}

Expand All @@ -175,6 +208,7 @@ export class ProsemirrorAdapter {
/>
</div>,
this.renderLinkMenu(),
this.renderPicker(),
];
}

Expand Down Expand Up @@ -202,6 +236,66 @@ export class ProsemirrorAdapter {
);
}

renderPicker() {
console.log('renderPicker');

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

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
if (!this.isPickerOpen) {
return;
}

console.log('picker is open');

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

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement

return (
<limel-portal
containerId={this.portalId}
visible={this.isPickerOpen}
openDirection="top"
inheritParentWidth={true}
anchor={this.actionBarElement}
>
<limel-picker
label="Favorite awesomenaut"
value={this.selectedItem}
searcher={this.search}
onChange={this.onChange}
onInteract={this.onInteract}
/>
</limel-portal>
);
}

private handleClosePicker = (event: CustomEvent<void>) => {
event.stopImmediatePropagation();
this.isPickerOpen = false;
};

private handleOpenPicker = (event: CustomEvent<string>) => {
event.stopImmediatePropagation();
this.isPickerOpen = true;
console.log('open picker', this.isPickerOpen);

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

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
};

private search = (query: string): Promise<ListItem[]> => {
return new Promise((resolve) => {
if (query === '') {
return resolve(this.fakeMentionItems);
}

const filteredItems = this.fakeMentionItems.filter((item) => {
return item.text.toLowerCase().includes(query.toLowerCase());
});

return resolve(filteredItems);
});
};

private onChange = (event: LimelPickerCustomEvent<ListItem<number>>) => {
this.selectedItem = event.detail;
};

private onInteract = (event: LimelPickerCustomEvent<ListItem<number>>) => {
console.log('Value interacted with:', event.detail);

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

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
};

private setupContentConverter() {
if (this.contentType === 'markdown') {
this.contentConverter = new markdownConverter();
Expand Down Expand Up @@ -256,6 +350,7 @@ export class ProsemirrorAdapter {
nodes: addListNodes(schema.spec.nodes, 'paragraph block*', 'block'),
marks: schema.spec.marks.append({
strikethrough: strikethrough,
mention: mentionTagMark,
}),
});
}
Expand Down Expand Up @@ -290,6 +385,7 @@ export class ProsemirrorAdapter {
this.updateActiveActionBarItems,
),
createActionBarInteractionPlugin(this.menuCommandFactory),
createTriggerPlugin(),
],
});
}
Expand Down

0 comments on commit f20bfee

Please sign in to comment.