499 lines
17 KiB
TypeScript
499 lines
17 KiB
TypeScript
import { BaseBlockHandler, type IBlockEventHandlers } from '../block.base.js';
|
|
import type { IBlock } from '../../wysiwyg.types.js';
|
|
import { cssManager } from '@design.estate/dees-element';
|
|
import { WysiwygBlocks } from '../../wysiwyg.blocks.js';
|
|
import { WysiwygSelection } from '../../wysiwyg.selection.js';
|
|
|
|
export class ParagraphBlockHandler extends BaseBlockHandler {
|
|
type = 'paragraph';
|
|
|
|
// Track cursor position
|
|
private lastKnownCursorPosition: number = 0;
|
|
private lastSelectedText: string = '';
|
|
private selectionHandler: (() => void) | null = null;
|
|
|
|
render(block: IBlock, isSelected: boolean): string {
|
|
const selectedClass = isSelected ? ' selected' : '';
|
|
const placeholder = this.getPlaceholder();
|
|
|
|
|
|
return `
|
|
<div
|
|
class="block paragraph${selectedClass}"
|
|
contenteditable="true"
|
|
data-placeholder="${placeholder}"
|
|
data-block-id="${block.id}"
|
|
data-block-type="${block.type}"
|
|
></div>
|
|
`;
|
|
}
|
|
|
|
setup(element: HTMLElement, block: IBlock, handlers: IBlockEventHandlers): void {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (!paragraphBlock) {
|
|
console.error('ParagraphBlockHandler.setup: No paragraph block element found');
|
|
return;
|
|
}
|
|
|
|
|
|
// Set initial content if needed
|
|
if (block.content && !paragraphBlock.innerHTML) {
|
|
paragraphBlock.innerHTML = block.content;
|
|
}
|
|
|
|
// Input handler with cursor tracking
|
|
paragraphBlock.addEventListener('input', (e) => {
|
|
handlers.onInput(e as InputEvent);
|
|
|
|
// Track cursor position after input
|
|
const pos = this.getCursorPosition(element);
|
|
if (pos !== null) {
|
|
this.lastKnownCursorPosition = pos;
|
|
}
|
|
});
|
|
|
|
// Keydown handler with cursor tracking
|
|
paragraphBlock.addEventListener('keydown', (e) => {
|
|
// Track cursor position before keydown
|
|
const pos = this.getCursorPosition(element);
|
|
if (pos !== null) {
|
|
this.lastKnownCursorPosition = pos;
|
|
}
|
|
|
|
handlers.onKeyDown(e);
|
|
});
|
|
|
|
// Focus handler
|
|
paragraphBlock.addEventListener('focus', () => {
|
|
handlers.onFocus();
|
|
});
|
|
|
|
// Blur handler
|
|
paragraphBlock.addEventListener('blur', () => {
|
|
handlers.onBlur();
|
|
});
|
|
|
|
// Composition handlers for IME support
|
|
paragraphBlock.addEventListener('compositionstart', () => {
|
|
handlers.onCompositionStart();
|
|
});
|
|
|
|
paragraphBlock.addEventListener('compositionend', () => {
|
|
handlers.onCompositionEnd();
|
|
});
|
|
|
|
// Mouse up handler
|
|
paragraphBlock.addEventListener('mouseup', (e) => {
|
|
const pos = this.getCursorPosition(element);
|
|
if (pos !== null) {
|
|
this.lastKnownCursorPosition = pos;
|
|
}
|
|
|
|
// Selection will be handled by selectionchange event
|
|
handlers.onMouseUp?.(e);
|
|
});
|
|
|
|
// Click handler with delayed cursor tracking
|
|
paragraphBlock.addEventListener('click', (e: MouseEvent) => {
|
|
// Small delay to let browser set cursor position
|
|
setTimeout(() => {
|
|
const pos = this.getCursorPosition(element);
|
|
if (pos !== null) {
|
|
this.lastKnownCursorPosition = pos;
|
|
}
|
|
}, 0);
|
|
});
|
|
|
|
// Keyup handler for additional cursor tracking
|
|
paragraphBlock.addEventListener('keyup', (e) => {
|
|
const pos = this.getCursorPosition(element);
|
|
if (pos !== null) {
|
|
this.lastKnownCursorPosition = pos;
|
|
}
|
|
});
|
|
|
|
// Set up selection change handler
|
|
this.setupSelectionHandler(element, paragraphBlock, block);
|
|
}
|
|
|
|
private setupSelectionHandler(element: HTMLElement, paragraphBlock: HTMLDivElement, block: IBlock): void {
|
|
// Add selection change handler
|
|
const checkSelection = () => {
|
|
const selection = window.getSelection();
|
|
if (!selection || selection.rangeCount === 0) return;
|
|
|
|
const selectedText = selection.toString();
|
|
if (selectedText.length === 0) {
|
|
// Clear selection if no text
|
|
if (this.lastSelectedText) {
|
|
this.lastSelectedText = '';
|
|
this.dispatchSelectionEvent(element, {
|
|
text: '',
|
|
blockId: block.id,
|
|
hasSelection: false
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Get parent wysiwyg component's shadow root - traverse from shadow root
|
|
const wysiwygBlock = (paragraphBlock.getRootNode() as ShadowRoot).host as any;
|
|
const parentComponent = wysiwygBlock?.closest('dees-input-wysiwyg');
|
|
const parentShadowRoot = parentComponent?.shadowRoot;
|
|
const blockShadowRoot = wysiwygBlock?.shadowRoot;
|
|
|
|
// Use getComposedRanges with shadow roots as per MDN docs
|
|
const shadowRoots: ShadowRoot[] = [];
|
|
if (parentShadowRoot) shadowRoots.push(parentShadowRoot);
|
|
if (blockShadowRoot) shadowRoots.push(blockShadowRoot);
|
|
|
|
// Get selection info using our Shadow DOM-aware utility
|
|
const selectionInfo = WysiwygSelection.getSelectionInfo(...shadowRoots);
|
|
if (!selectionInfo) return;
|
|
|
|
// Check if selection is within this block
|
|
const startInBlock = WysiwygSelection.containsAcrossShadowDOM(paragraphBlock, selectionInfo.startContainer);
|
|
const endInBlock = WysiwygSelection.containsAcrossShadowDOM(paragraphBlock, selectionInfo.endContainer);
|
|
|
|
if (startInBlock || endInBlock) {
|
|
if (selectedText !== this.lastSelectedText) {
|
|
this.lastSelectedText = selectedText;
|
|
|
|
// Create range and get rect
|
|
const range = WysiwygSelection.createRangeFromInfo(selectionInfo);
|
|
const rect = range.getBoundingClientRect();
|
|
|
|
// Dispatch event
|
|
this.dispatchSelectionEvent(element, {
|
|
text: selectedText.trim(),
|
|
blockId: block.id,
|
|
range: range,
|
|
rect: rect,
|
|
hasSelection: true
|
|
});
|
|
}
|
|
} else if (this.lastSelectedText) {
|
|
// Clear selection if no longer in this block
|
|
this.lastSelectedText = '';
|
|
this.dispatchSelectionEvent(element, {
|
|
text: '',
|
|
blockId: block.id,
|
|
hasSelection: false
|
|
});
|
|
}
|
|
};
|
|
|
|
// Listen for selection changes
|
|
document.addEventListener('selectionchange', checkSelection);
|
|
|
|
// Store the handler for cleanup
|
|
this.selectionHandler = checkSelection;
|
|
|
|
// Clean up on disconnect (will be called by dees-wysiwyg-block)
|
|
const wysiwygBlock = element.closest('dees-wysiwyg-block');
|
|
if (wysiwygBlock) {
|
|
const originalDisconnectedCallback = (wysiwygBlock as any).disconnectedCallback;
|
|
(wysiwygBlock as any).disconnectedCallback = async function() {
|
|
if (this.selectionHandler) {
|
|
document.removeEventListener('selectionchange', this.selectionHandler);
|
|
this.selectionHandler = null;
|
|
}
|
|
if (originalDisconnectedCallback) {
|
|
await originalDisconnectedCallback.call(wysiwygBlock);
|
|
}
|
|
}.bind(this);
|
|
}
|
|
}
|
|
|
|
private dispatchSelectionEvent(element: HTMLElement, detail: any): void {
|
|
const event = new CustomEvent('block-text-selected', {
|
|
detail,
|
|
bubbles: true,
|
|
composed: true
|
|
});
|
|
element.dispatchEvent(event);
|
|
}
|
|
|
|
getStyles(): string {
|
|
return `
|
|
/* Paragraph specific styles */
|
|
.block.paragraph {
|
|
font-size: 16px;
|
|
line-height: 1.6;
|
|
font-weight: 400;
|
|
}
|
|
`;
|
|
}
|
|
|
|
getPlaceholder(): string {
|
|
return "Type '/' for commands...";
|
|
}
|
|
|
|
/**
|
|
* Helper to get the last text node in an element
|
|
*/
|
|
private getLastTextNode(element: Node): Text | null {
|
|
if (element.nodeType === Node.TEXT_NODE) {
|
|
return element as Text;
|
|
}
|
|
|
|
for (let i = element.childNodes.length - 1; i >= 0; i--) {
|
|
const lastText = this.getLastTextNode(element.childNodes[i]);
|
|
if (lastText) return lastText;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// Helper methods for paragraph functionality
|
|
|
|
getCursorPosition(element: HTMLElement, context?: any): number | null {
|
|
// Get the actual paragraph element
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (!paragraphBlock) {
|
|
return null;
|
|
}
|
|
|
|
// Get shadow roots from context
|
|
const wysiwygBlock = context?.component;
|
|
const parentComponent = wysiwygBlock?.closest('dees-input-wysiwyg');
|
|
const parentShadowRoot = parentComponent?.shadowRoot;
|
|
const blockShadowRoot = context?.shadowRoot;
|
|
|
|
// Get selection info with both shadow roots for proper traversal
|
|
const shadowRoots: ShadowRoot[] = [];
|
|
if (parentShadowRoot) shadowRoots.push(parentShadowRoot);
|
|
if (blockShadowRoot) shadowRoots.push(blockShadowRoot);
|
|
|
|
const selectionInfo = WysiwygSelection.getSelectionInfo(...shadowRoots);
|
|
|
|
if (!selectionInfo) {
|
|
return null;
|
|
}
|
|
|
|
if (!WysiwygSelection.containsAcrossShadowDOM(paragraphBlock, selectionInfo.startContainer)) {
|
|
return null;
|
|
}
|
|
|
|
// Create a range from start of element to cursor position
|
|
const preCaretRange = document.createRange();
|
|
preCaretRange.selectNodeContents(paragraphBlock);
|
|
preCaretRange.setEnd(selectionInfo.startContainer, selectionInfo.startOffset);
|
|
|
|
// Get the text content length up to cursor
|
|
const position = preCaretRange.toString().length;
|
|
|
|
return position;
|
|
}
|
|
|
|
getContent(element: HTMLElement, context?: any): string {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (!paragraphBlock) return '';
|
|
|
|
// For paragraphs, get the innerHTML which includes formatting tags
|
|
const content = paragraphBlock.innerHTML || '';
|
|
return content;
|
|
}
|
|
|
|
setContent(element: HTMLElement, content: string, context?: any): void {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (!paragraphBlock) return;
|
|
|
|
// Store if we have focus
|
|
const hadFocus = document.activeElement === paragraphBlock ||
|
|
element.shadowRoot?.activeElement === paragraphBlock;
|
|
|
|
paragraphBlock.innerHTML = content;
|
|
|
|
// Restore focus if we had it
|
|
if (hadFocus) {
|
|
paragraphBlock.focus();
|
|
}
|
|
}
|
|
|
|
setCursorToStart(element: HTMLElement, context?: any): void {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (paragraphBlock) {
|
|
WysiwygBlocks.setCursorToStart(paragraphBlock);
|
|
}
|
|
}
|
|
|
|
setCursorToEnd(element: HTMLElement, context?: any): void {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (paragraphBlock) {
|
|
WysiwygBlocks.setCursorToEnd(paragraphBlock);
|
|
}
|
|
}
|
|
|
|
focus(element: HTMLElement, context?: any): void {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (!paragraphBlock) return;
|
|
|
|
// Ensure the element is focusable
|
|
if (!paragraphBlock.hasAttribute('contenteditable')) {
|
|
paragraphBlock.setAttribute('contenteditable', 'true');
|
|
}
|
|
|
|
paragraphBlock.focus();
|
|
|
|
// If focus failed, try again after a microtask
|
|
if (document.activeElement !== paragraphBlock && element.shadowRoot?.activeElement !== paragraphBlock) {
|
|
Promise.resolve().then(() => {
|
|
paragraphBlock.focus();
|
|
});
|
|
}
|
|
}
|
|
|
|
focusWithCursor(element: HTMLElement, position: 'start' | 'end' | number = 'end', context?: any): void {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (!paragraphBlock) return;
|
|
|
|
|
|
// Ensure element is focusable first
|
|
if (!paragraphBlock.hasAttribute('contenteditable')) {
|
|
paragraphBlock.setAttribute('contenteditable', 'true');
|
|
}
|
|
|
|
// For 'end' position, we need to set up selection before focus to prevent browser default
|
|
if (position === 'end' && paragraphBlock.textContent && paragraphBlock.textContent.length > 0) {
|
|
// Set up the selection first
|
|
const sel = window.getSelection();
|
|
if (sel) {
|
|
const range = document.createRange();
|
|
const lastNode = this.getLastTextNode(paragraphBlock) || paragraphBlock;
|
|
if (lastNode.nodeType === Node.TEXT_NODE) {
|
|
range.setStart(lastNode, lastNode.textContent?.length || 0);
|
|
range.setEnd(lastNode, lastNode.textContent?.length || 0);
|
|
} else {
|
|
range.selectNodeContents(lastNode);
|
|
range.collapse(false);
|
|
}
|
|
sel.removeAllRanges();
|
|
sel.addRange(range);
|
|
}
|
|
}
|
|
|
|
// Now focus the element
|
|
paragraphBlock.focus();
|
|
|
|
// Set cursor position after focus is established (for non-end positions)
|
|
const setCursor = () => {
|
|
if (position === 'start') {
|
|
this.setCursorToStart(element, context);
|
|
} else if (position === 'end' && (!paragraphBlock.textContent || paragraphBlock.textContent.length === 0)) {
|
|
// Only call setCursorToEnd for empty blocks
|
|
this.setCursorToEnd(element, context);
|
|
} else if (typeof position === 'number') {
|
|
// Use the selection utility to set cursor position
|
|
WysiwygSelection.setCursorPosition(paragraphBlock, position);
|
|
}
|
|
};
|
|
|
|
// Ensure cursor is set after focus
|
|
if (document.activeElement === paragraphBlock || element.shadowRoot?.activeElement === paragraphBlock) {
|
|
setCursor();
|
|
} else {
|
|
// Wait for focus to be established
|
|
Promise.resolve().then(() => {
|
|
if (document.activeElement === paragraphBlock || element.shadowRoot?.activeElement === paragraphBlock) {
|
|
setCursor();
|
|
} else {
|
|
// Try again with a small delay - sometimes focus needs more time
|
|
setTimeout(() => {
|
|
if (document.activeElement === paragraphBlock || element.shadowRoot?.activeElement === paragraphBlock) {
|
|
setCursor();
|
|
}
|
|
}, 10);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
getSplitContent(element: HTMLElement, context?: any): { before: string; after: string } | null {
|
|
const paragraphBlock = element.querySelector('.block.paragraph') as HTMLDivElement;
|
|
if (!paragraphBlock) {
|
|
return null;
|
|
}
|
|
|
|
// Get shadow roots from context
|
|
const wysiwygBlock = context?.component;
|
|
const parentComponent = wysiwygBlock?.closest('dees-input-wysiwyg');
|
|
const parentShadowRoot = parentComponent?.shadowRoot;
|
|
const blockShadowRoot = context?.shadowRoot;
|
|
|
|
// Get selection info with both shadow roots for proper traversal
|
|
const shadowRoots: ShadowRoot[] = [];
|
|
if (parentShadowRoot) shadowRoots.push(parentShadowRoot);
|
|
if (blockShadowRoot) shadowRoots.push(blockShadowRoot);
|
|
|
|
const selectionInfo = WysiwygSelection.getSelectionInfo(...shadowRoots);
|
|
|
|
if (!selectionInfo) {
|
|
// Try using last known cursor position
|
|
if (this.lastKnownCursorPosition !== null) {
|
|
const fullText = paragraphBlock.textContent || '';
|
|
const pos = Math.min(this.lastKnownCursorPosition, fullText.length);
|
|
return {
|
|
before: fullText.substring(0, pos),
|
|
after: fullText.substring(pos)
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Make sure the selection is within this block
|
|
if (!WysiwygSelection.containsAcrossShadowDOM(paragraphBlock, selectionInfo.startContainer)) {
|
|
// Try using last known cursor position
|
|
if (this.lastKnownCursorPosition !== null) {
|
|
const fullText = paragraphBlock.textContent || '';
|
|
const pos = Math.min(this.lastKnownCursorPosition, fullText.length);
|
|
return {
|
|
before: fullText.substring(0, pos),
|
|
after: fullText.substring(pos)
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Get cursor position first
|
|
const cursorPos = this.getCursorPosition(element, context);
|
|
|
|
if (cursorPos === null || cursorPos === 0) {
|
|
// If cursor is at start or can't determine position, move all content
|
|
return {
|
|
before: '',
|
|
after: paragraphBlock.innerHTML
|
|
};
|
|
}
|
|
|
|
// For HTML content, split using ranges to preserve formatting
|
|
const beforeRange = document.createRange();
|
|
const afterRange = document.createRange();
|
|
|
|
// Before range: from start of element to cursor
|
|
beforeRange.setStart(paragraphBlock, 0);
|
|
beforeRange.setEnd(selectionInfo.startContainer, selectionInfo.startOffset);
|
|
|
|
// After range: from cursor to end of element
|
|
afterRange.setStart(selectionInfo.startContainer, selectionInfo.startOffset);
|
|
afterRange.setEnd(paragraphBlock, paragraphBlock.childNodes.length);
|
|
|
|
// Extract HTML content
|
|
const beforeFragment = beforeRange.cloneContents();
|
|
const afterFragment = afterRange.cloneContents();
|
|
|
|
// Convert to HTML strings
|
|
const tempDiv = document.createElement('div');
|
|
tempDiv.appendChild(beforeFragment);
|
|
const beforeHtml = tempDiv.innerHTML;
|
|
|
|
tempDiv.innerHTML = '';
|
|
tempDiv.appendChild(afterFragment);
|
|
const afterHtml = tempDiv.innerHTML;
|
|
|
|
return {
|
|
before: beforeHtml,
|
|
after: afterHtml
|
|
};
|
|
}
|
|
} |