Skip to content
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

Tiptap: Render toolbar with row and group configuration #2374

Merged
merged 5 commits into from
Sep 30, 2024
Merged
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
@@ -1,4 +1,4 @@
import type { UmbTiptapExtensionApi } from '../../extensions/types.js';
import type { UmbTiptapExtensionApi, UmbTiptapToolbarValue } from '../../extensions/types.js';
import { css, customElement, html, property, state, when } from '@umbraco-cms/backoffice/external/lit';
import { loadManifestApi } from '@umbraco-cms/backoffice/extension-api';
import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry';
Expand Down Expand Up @@ -75,7 +75,7 @@ export class UmbInputTiptapElement extends UmbFormControlMixin<string, typeof Um
private _editor!: Editor;

@state()
_toolbar: string[][][] = [[[]]];
_toolbar: UmbTiptapToolbarValue = [[[]]];

protected override async firstUpdated() {
await Promise.all([await this.#loadExtensions(), await this.#loadEditor()]);
Expand Down Expand Up @@ -110,7 +110,7 @@ export class UmbInputTiptapElement extends UmbFormControlMixin<string, typeof Um
if (maxWidth) this.setAttribute('style', `max-width: ${maxWidth}px;`);
if (maxHeight) element.setAttribute('style', `max-height: ${maxHeight}px;`);

this._toolbar = this.configuration?.getValueByAlias<string[][][]>('toolbar') ?? [[[]]];
this._toolbar = this.configuration?.getValueByAlias<UmbTiptapToolbarValue>('toolbar') ?? [[[]]];

const extensions = this._extensions
.map((ext) => ext.getTiptapExtensions({ configuration: this.configuration }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ManifestTiptapToolbarExtension } from '../../extensions/tiptap-toolbar-extension.js';
import { css, customElement, html, property } from '@umbraco-cms/backoffice/external/lit';
import type { UmbTiptapToolbarValue } from '../../extensions/types.js';
import { css, customElement, html, map, property, state } from '@umbraco-cms/backoffice/external/lit';
import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry';
import { UmbExtensionsElementAndApiInitializer } from '@umbraco-cms/backoffice/extension-api';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import type { Editor } from '@umbraco-cms/backoffice/external/tiptap';
import type { UmbPropertyEditorConfigCollection } from '@umbraco-cms/backoffice/property-editor';
Expand All @@ -10,6 +12,12 @@ const elementName = 'umb-tiptap-fixed-menu';

@customElement(elementName)
export class UmbTiptapFixedMenuElement extends UmbLitElement {
#attached = false;
#extensionsController?: UmbExtensionsElementAndApiInitializer;

@state()
private _lookup?: Map<string, unknown>;

@property({ type: Boolean, reflect: true })
readonly = false;

Expand All @@ -20,21 +28,61 @@ export class UmbTiptapFixedMenuElement extends UmbLitElement {
configuration?: UmbPropertyEditorConfigCollection;

@property({ attribute: false })
toolbar: string[][][] = [[[]]];
toolbar: UmbTiptapToolbarValue = [[[]]];

override connectedCallback(): void {
super.connectedCallback();
this.#attached = true;
this.#observeExtensions();
}
override disconnectedCallback(): void {
this.#attached = false;
this.#extensionsController?.destroy();
this.#extensionsController = undefined;
super.disconnectedCallback();
}

#observeExtensions(): void {
if (!this.#attached) return;
this.#extensionsController?.destroy();

this.#extensionsController = new UmbExtensionsElementAndApiInitializer(
this,
umbExtensionsRegistry,
'tiptapToolbarExtension',
[],
(manifest) => this.toolbar.flat(2).includes(manifest.alias),
(extensionControllers) => {
this._lookup = new Map(extensionControllers.map((ext) => [ext.alias, ext.component]));
},
);

this.#extensionsController.apiProperties = { configuration: this.configuration };
this.#extensionsController.elementProperties = { editor: this.editor, configuration: this.configuration };
}

override render() {
return html`
<umb-extension-with-api-slot
type="tiptapToolbarExtension"
.filter=${(ext: ManifestTiptapToolbarExtension) =>
this.toolbar.flat(2).includes(ext.alias) && (!!ext.kind || !!ext.element)}
.elementProps=${{ editor: this.editor, configuration: this.configuration }}
.apiProps=${{ configuration: this.configuration }}>
</umb-extension-with-api-slot>
`;
return html`${map(this.toolbar, (row, rowIndex) =>
map(
row,
(group, groupIndex) =>
html`${map(group, (alias, aliasIndex) => {
const newRow = rowIndex !== 0 && groupIndex === 0 && aliasIndex === 0;
return html`<div class="item" ?data-new-row=${newRow} style="${newRow ? 'grid-column: 1 / span 3' : ''}">
${this._lookup?.get(alias)}
</div>`;
})}
<div class="separator"></div> `,
),
)} `;
}

static override readonly styles = css`
:host([readonly]) {
pointer-events: none;
background-color: var(--uui-color-surface-alt);
}

:host {
border-radius: var(--uui-border-radius);
border: 1px solid var(--uui-color-border);
Expand All @@ -43,20 +91,29 @@ export class UmbTiptapFixedMenuElement extends UmbLitElement {
background-color: var(--uui-color-surface);
color: var(--color-text);
display: grid;
grid-template-columns: repeat(auto-fill, minmax(24px, 1fr));
gap: var(--uui-size-space-1);
grid-template-columns: repeat(auto-fill, minmax(10px, 1fr));
grid-auto-flow: row;
position: sticky;
top: -25px;
left: 0px;
right: 0px;
padding: var(--uui-size-space-3);
align-items: center;
z-index: 9999999;
}

:host([readonly]) {
pointer-events: none;
background-color: var(--uui-color-surface-alt);
.item {
grid-column: span 3;
}

.separator {
background-color: var(--uui-color-border);
width: 1px;
place-self: center;
height: 22px;
}
.separator:last-child,
.separator:has(+ [data-new-row]) {
display: none;
}
`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/packages/rte/tiptap/extensions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ export abstract class UmbTiptapToolbarElementApiBase extends UmbControllerBase i
return editor && this.manifest?.meta.alias ? editor?.isActive(this.manifest.meta.alias) : false;
}
}

export type UmbTiptapToolbarValue = Array<Array<Array<string>>>;
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import {
customElement,
css,
html,
property,
state,
repeat,
nothing,
type PropertyValueMap,
} from '@umbraco-cms/backoffice/external/lit';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import type { UmbTiptapToolbarValue } from '../extensions/types.js';
import { customElement, css, html, property, state, repeat, nothing } from '@umbraco-cms/backoffice/external/lit';
import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import { UmbPropertyValueChangeEvent, type UmbPropertyEditorUiElement } from '@umbraco-cms/backoffice/property-editor';
import type { PropertyValueMap } from '@umbraco-cms/backoffice/external/lit';

type Extension = {
alias: string;
Expand All @@ -25,7 +18,7 @@ export class UmbPropertyEditorUiTiptapToolbarConfigurationElement
implements UmbPropertyEditorUiElement
{
@property({ attribute: false })
set value(value: string[][][] | undefined) {
set value(value: UmbTiptapToolbarValue | undefined) {
if (!value) {
this.#useDefault = true;
this.#value = [[[]]];
Expand All @@ -36,14 +29,14 @@ export class UmbPropertyEditorUiTiptapToolbarConfigurationElement
this.#value = value.map((rows) => rows.map((groups) => [...groups]));
}

get value(): string[][][] {
get value(): UmbTiptapToolbarValue {
// TODO: This can be optimized with cashing;
return this.#value.map((rows) => rows.map((groups) => [...groups]));
}

#useDefault = false;

#value: string[][][] = [[[]]];
#value: UmbTiptapToolbarValue = [[[]]];

@state()
_extensions: Extension[] = [];
Expand Down