Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125// A few paragraphs of rich text (a brief, a plan): a small TipTap document// of paragraphs and lists with bold, italic, code and links, content in and// out as ProseMirror block JSON. Bigger than <draft-line>, lighter than// <draft-editor>: no sections, no suggestions, no ids.import { LitElement, html, type PropertyValues } from 'lit';import { Editor } from '@tiptap/core';import StarterKit from '@tiptap/starter-kit';import { BubbleMenu } from '@tiptap/extension-bubble-menu';import { Placeholder } from '@tiptap/extensions';import { fillBubble, type InlineFeature } from './bubble.ts';import type { PMJson } from '../types.ts';
const FEATURES: readonly InlineFeature[] = ['bold', 'italic', 'code'];
const stable = (content: PMJson[]) => JSON.stringify(content);
export class DraftText extends LitElement { static properties = { content: { attribute: false }, placeholder: { type: String } }; /** The blocks (paragraphs, lists). */ declare content: PMJson[]; declare placeholder: string;
#editor: Editor | null = null; #lastEmitted = ''; #bubble: { update: () => void } | null = null;
constructor() { super(); this.content = []; this.placeholder = ''; }
protected override createRenderRoot() { return this; }
get editor(): Editor | null { return this.#editor; }
#doc(content: PMJson[]): PMJson { return { type: 'doc', content: content.length ? content : [{ type: 'paragraph' }] }; }
override firstUpdated(): void { const surface = this.querySelector('.surface') as HTMLElement; const bubbleEl = document.createElement('div'); const editor = new Editor({ element: surface, extensions: [ StarterKit.configure({ heading: false, blockquote: false, horizontalRule: false, strike: false, underline: false, codeBlock: false, trailingNode: false, dropcursor: false, gapcursor: false, link: { openOnClick: false, autolink: true, defaultProtocol: 'https' }, }), Placeholder.configure({ placeholder: () => this.placeholder }), BubbleMenu.configure({ element: bubbleEl, appendTo: () => this.querySelector('.chrome') as HTMLElement, shouldShow: ({ from, to }) => from !== to, options: { placement: 'top-start' }, }), ], content: this.#doc(this.content) as never, editorProps: { attributes: { class: 'text', spellcheck: 'true' } }, onUpdate: ({ editor: e }) => { const json = e.getJSON() as PMJson; const content = json.content ?? []; this.#lastEmitted = stable(content); this.content = content; this.dispatchEvent(new CustomEvent<PMJson[]>('change', { detail: content })); }, onSelectionUpdate: () => this.#bubble?.update(), }); this.#editor = editor; if (this.#focusOnce) { editor.chain().focus(this.#focusOnce).run(); this.#focusOnce = null; } this.#bubble = fillBubble(bubbleEl, editor, FEATURES, { onLink: () => {}, onRef: () => {} }); bubbleEl.style.visibility = 'hidden'; this.#lastEmitted = stable(this.content); }
override updated(changed: PropertyValues): void { if (changed.has('content') && this.#editor && stable(this.content) !== this.#lastEmitted) { this.#editor.commands.setContent(this.#doc(this.content) as never, { emitUpdate: false }); this.#lastEmitted = stable(this.content); } }
override disconnectedCallback(): void { super.disconnectedCallback(); this.#editor?.destroy(); this.#editor = null; }
/** A focus asked for before the editor exists (a host focusing a box it has just put up) is kept until it does. */ #focusOnce: 'start' | 'end' | null = null;
focusEnd(): void { if (this.#editor) this.#editor.chain().focus('end').run(); else this.#focusOnce = 'end'; }
focusStart(): void { if (this.#editor) this.#editor.chain().focus('start').run(); else this.#focusOnce = 'start'; }
override render() { return html`<div class="surface"></div> <div class="chrome"></div>`; }}
customElements.define('draft-text', DraftText);