import { ChangeDetectionStrategy, Component, ElementRef, computed, effect, inject, input, signal, viewChild, } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { DocumentType, Editor, NodeType } from '@tiptap/core'; import Blockquote from '@tiptap/extension-blockquote'; import Bold from '@tiptap/extension-bold'; import Code from '@tiptap/extension-code'; import CodeBlock from '@tiptap/extension-code-block'; import Document from '@tiptap/extension-document'; import Heading from '@tiptap/extension-heading'; import Italic from '@tiptap/extension-italic'; import Link from '@tiptap/extension-link'; import { BulletList, ListItem } from '@tiptap/extension-list'; import Paragraph from '@tiptap/extension-paragraph'; import Strike from '@tiptap/extension-strike'; import Text from '@tiptap/extension-text'; import TextAlign from '@tiptap/extension-text-align'; import Underline from '@tiptap/extension-underline'; import { Dropcursor, Gapcursor, UndoRedo } from '@tiptap/extensions'; import { Node as ProseNode } from 'prosemirror-model'; import { ButtonComponent, ButtonGroupComponent, ButtonGroupsComponent, ButtonHotkeyComponent, DropdownComponent, DuiDialog, FilesystemApi, FilesystemFileDialog, InputComponent, OpenDropdownHoverDirective, ValueAccessorBase, ngValueAccessor, } from '@deepkit/desktop-ui'; import { Image } from './text-editor-image'; @Component({ selector: 'app-text-editor', imports: [ ButtonGroupsComponent, ButtonGroupComponent, ButtonComponent, DropdownComponent, FormsModule, InputComponent, OpenDropdownHoverDirective, ButtonHotkeyComponent, ], template: `
Body
H1
H2
H3
Code
Bold
Italic
Underline
Strike
Bullet List
Code Block
Code
Align Left
Align Center
Align Right
Align Justify
Body H1 H2 H3 @if (selection(); as selection) { @if (selection.node.type.name === 'image') {
Change
} }
`, styleUrl: './text-editor.component.scss', changeDetection: ChangeDetectionStrategy.Eager, providers: [ngValueAccessor(TextEditorComponent)], }) export class TextEditorComponent extends ValueAccessorBase { editorContainer = viewChild.required('editorContainer', { read: ElementRef }); imageToolbar = viewChild.required('imageToolbar', { read: DropdownComponent }); filesystemApi = input.required(); changed = signal(0); selection = signal<{ node: ProseNode; element: HTMLElement } | undefined>(undefined); protected fromEditor?: NodeType[]; editor = computed(() => { const editor = new Editor({ element: this.editorContainer().nativeElement, extensions: [ UndoRedo, Gapcursor, Document, Paragraph, Text, Dropcursor, BulletList, ListItem, Blockquote, Code, Link.configure({ openOnClick: false, enableClickSelection: true, }), TextAlign.configure({ types: ['heading', 'paragraph', 'image'], }), Bold, Heading, Italic, Underline, Strike, CodeBlock.configure({ defaultLanguage: 'typescript', languageClassPrefix: 'language-', }), Image.configure({ // inline: true, textEditor: this, }), ], }); editor.on('update', props => { const value = props.editor.getJSON() as DocumentType; this.fromEditor = value.content; this.changed.update(v => ++v); this.setValue(value.content); }); editor.on('selectionUpdate', props => { this.changed.update(v => ++v); const node = props.editor.state.selection.$head.nodeBefore || undefined; const element = props.editor.view.nodeDOM(props.editor.state.selection.anchor) || props.editor.view.domAtPos(props.editor.state.selection.anchor, 0)?.node; this.selection.set(node && element ? { node, element: element as HTMLElement } : undefined); // console.log('selectionUpdate', node, element); // console.log('props.editor.state.selection.$head', props.editor.state.selection.$head); }); return editor; }); header = this.trackChange(editor => editor.getAttributes('heading')?.level || 0); image = this.trackChange(editor => editor.isActive('image')); format = this.trackChange(editor => { return ( '' + (editor.isActive('bold') && 'bold') + (editor.isActive('italic') && 'italic') + (editor.isActive('underline') && 'underline') + (editor.isActive('strike') && 'strike') ); }); aligned = this.trackChange(editor => { if (editor.isActive({ textAlign: 'center' })) return 'center'; if (editor.isActive({ textAlign: 'right' })) return 'right'; return 'left'; }); codeBlock = this.trackChange(editor => editor.isActive('codeBlock')); code = this.trackChange(editor => editor.isActive('code')); link = this.trackChange(editor => editor.isActive('link')); dialog = inject(DuiDialog); constructor() { super(); effect(() => { const editor = this.editor(); const value = this.value(); if (this.fromEditor === value) return; if (!value || !value.length) { this.editor().commands.clearContent(); return; } editor.commands.setContent(value); }); } async addImage() { const api = this.filesystemApi(); const path = await this.dialog.open(FilesystemFileDialog, { api }, { backDropCloses: true }).close; if (!path) return; this.editor() .chain() .insertContent({ type: 'image', attrs: { path: path } }) .focus() .run(); } async changeImage() { const api = this.filesystemApi(); const path = await this.dialog.open(FilesystemFileDialog, { api }, { backDropCloses: true }).close; if (!path) return; this.editor().chain().updateAttributes('image', { path: path }).run(); } trackChange(callback: (editor: Editor) => T) { return computed(() => { this.changed(); return callback(this.editor()); }); } undo() { this.editor().chain().focus().undo().run(); } redo() { this.editor().chain().focus().redo().run(); } updateAttribute(node: ProseNode, name: string, value: any) { this.editor() .chain() .updateAttributes(node.type.name, { [name]: value }) .run(); } toggleCodeBlock() { this.editor().chain().focus().toggleCodeBlock().run(); } toggleCode() { this.editor().chain().focus().toggleCode().run(); } toggleLink() { if (this.editor().isActive('link')) { this.editor().chain().focus().unsetLink().run(); } else { const href = prompt('Enter link URL:', '') || ''; if (!href) return; this.editor().chain().focus().toggleLink({ href }).run(); } } setHeader(h: 0 | 1 | 2 | 3) { if (!h) { this.editor().chain().setParagraph().focus().run(); } else { this.editor().chain().setHeading({ level: h }).focus().run(); } } }