@stackch/angular-material-richtext-editor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2132 @@
1
+ import * as i0 from '@angular/core';
2
+ import { EventEmitter, Output, Input, Component, forwardRef, HostListener, ViewChild } from '@angular/core';
3
+ import { CommonModule } from '@angular/common';
4
+ import { NG_VALUE_ACCESSOR } from '@angular/forms';
5
+ import * as i1 from '@angular/material/button';
6
+ import { MatButtonModule } from '@angular/material/button';
7
+ import * as i2 from '@angular/material/icon';
8
+ import { MatIconModule } from '@angular/material/icon';
9
+ import * as i3 from '@angular/material/menu';
10
+ import { MatMenuModule } from '@angular/material/menu';
11
+ import * as i4 from '@angular/material/tooltip';
12
+ import { MatTooltipModule } from '@angular/material/tooltip';
13
+ import { MatFormFieldModule } from '@angular/material/form-field';
14
+ import { MatSelectModule } from '@angular/material/select';
15
+
16
+ class StackchRichtextEditorMaterialToolbar {
17
+ // State/config from parent editor
18
+ cfg;
19
+ i18n;
20
+ disabled = false;
21
+ fonts = [];
22
+ fontSizes = [];
23
+ isBoldActive = false;
24
+ isItalicActive = false;
25
+ isUnderlineActive = false;
26
+ canUndo = false;
27
+ canRedo = false;
28
+ // Consolidated UI state
29
+ uiState;
30
+ // Actions -> bubble to parent editor
31
+ undo = new EventEmitter();
32
+ redo = new EventEmitter();
33
+ toggleFontPanel = new EventEmitter();
34
+ toggleHeadingMenu = new EventEmitter();
35
+ toggleSpacingMenu = new EventEmitter();
36
+ toggleAlignMenu = new EventEmitter();
37
+ toggleColorMenu = new EventEmitter();
38
+ toggleListMenu = new EventEmitter();
39
+ pickFont = new EventEmitter();
40
+ pickFontSize = new EventEmitter();
41
+ pickAlign = new EventEmitter();
42
+ pickList = new EventEmitter();
43
+ pickHeading = new EventEmitter();
44
+ pickSpacing = new EventEmitter();
45
+ applyColor = new EventEmitter();
46
+ applyHighlight = new EventEmitter();
47
+ toggleBold = new EventEmitter();
48
+ toggleItalic = new EventEmitter();
49
+ toggleUnderline = new EventEmitter();
50
+ insertLink = new EventEmitter();
51
+ removeFormat = new EventEmitter();
52
+ // Ask parent to preserve selection before toolbar interactions (mousedown/pointerdown/focus)
53
+ saveSelectionRequest = new EventEmitter();
54
+ onToolbarMouseDown(evt) {
55
+ // Prevent focus stealing and keep selection in the editor
56
+ evt.preventDefault();
57
+ evt.stopPropagation();
58
+ this.saveSelectionRequest.emit();
59
+ }
60
+ onColorPointerDown(evt) {
61
+ // Do not preventDefault to allow the color picker to open, just preserve selection
62
+ evt.stopPropagation();
63
+ this.saveSelectionRequest.emit();
64
+ }
65
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: StackchRichtextEditorMaterialToolbar, deps: [], target: i0.ɵɵFactoryTarget.Component });
66
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.4", type: StackchRichtextEditorMaterialToolbar, isStandalone: true, selector: "stackch-richtext-editor-material-toolbar", inputs: { cfg: "cfg", i18n: "i18n", disabled: "disabled", fonts: "fonts", fontSizes: "fontSizes", isBoldActive: "isBoldActive", isItalicActive: "isItalicActive", isUnderlineActive: "isUnderlineActive", canUndo: "canUndo", canRedo: "canRedo", uiState: "uiState" }, outputs: { undo: "undo", redo: "redo", toggleFontPanel: "toggleFontPanel", toggleHeadingMenu: "toggleHeadingMenu", toggleSpacingMenu: "toggleSpacingMenu", toggleAlignMenu: "toggleAlignMenu", toggleColorMenu: "toggleColorMenu", toggleListMenu: "toggleListMenu", pickFont: "pickFont", pickFontSize: "pickFontSize", pickAlign: "pickAlign", pickList: "pickList", pickHeading: "pickHeading", pickSpacing: "pickSpacing", applyColor: "applyColor", applyHighlight: "applyHighlight", toggleBold: "toggleBold", toggleItalic: "toggleItalic", toggleUnderline: "toggleUnderline", insertLink: "insertLink", removeFormat: "removeFormat", saveSelectionRequest: "saveSelectionRequest" }, ngImport: i0, template: "<div class=\"stackch_rte__toolbar\">\r\n <!-- Undo / Redo -->\r\n @if (cfg.showUndoRedo) {\r\n <button mat-icon-button (click)=\"undo.emit()\" [disabled]=\"disabled || !canUndo\" [matTooltip]=\"i18n.undoTitle\" aria-label=\"{{i18n.undoTitle}}\">\r\n <mat-icon fontIcon=\"undo\"></mat-icon>\r\n </button>\r\n <button mat-icon-button (click)=\"redo.emit()\" [disabled]=\"disabled || !canRedo\" [matTooltip]=\"i18n.redoTitle\" aria-label=\"{{i18n.redoTitle}}\">\r\n <mat-icon fontIcon=\"redo\"></mat-icon>\r\n </button>\r\n <span class=\"stackch_rte__sep\"></span>\r\n }\r\n\r\n <!-- Font & Size: single icon opens a menu with two submenus (Fonts, Sizes) -->\r\n @if (cfg.showFontPanel) {\r\n <button mat-icon-button [matMenuTriggerFor]=\"fontPanelMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.fontPanelTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.fontPanelTitle }}\">\r\n <mat-icon fontIcon=\"text_fields\"></mat-icon>\r\n </button>\r\n <mat-menu #fontPanelMenu=\"matMenu\">\r\n <button mat-menu-item [matMenuTriggerFor]=\"fontMenu\">\r\n <mat-icon fontIcon=\"text_fields\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.fontSectionTitle }}</span>\r\n </button>\r\n <button mat-menu-item [matMenuTriggerFor]=\"sizeMenu\">\r\n <mat-icon fontIcon=\"format_size\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.sizeSectionTitle }}</span>\r\n </button>\r\n </mat-menu>\r\n\r\n <mat-menu #fontMenu=\"matMenu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n @for (f of fonts; track f) {\r\n <button mat-menu-item (click)=\"pickFont.emit(f)\"><span [style.fontFamily]=\"f\">{{ f }}</span></button>\r\n }\r\n </mat-menu>\r\n\r\n <mat-menu #sizeMenu=\"matMenu\" class=\"stackch_rte__size-menu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <div style=\"display:grid; grid-template-columns: repeat(3, auto); gap:.25rem .5rem; padding:.25rem .5rem;\">\r\n @for (s of fontSizes; track s) {\r\n <button mat-menu-item style=\"min-width:auto; padding:0 .5rem; line-height:28px; height:28px;\" (click)=\"pickFontSize.emit(s)\">{{ s }}px</button>\r\n }\r\n </div>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Headings menu -->\r\n @if (cfg.showHeading) {\r\n <button mat-button [matMenuTriggerFor]=\"headingMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.headingTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.headingTitle }}\">\r\n <mat-icon fontIcon=\"title\"></mat-icon>\r\n </button>\r\n <mat-menu #headingMenu=\"matMenu\">\r\n <button mat-menu-item (click)=\"pickHeading.emit('p')\">{{ i18n.paragraphLabel }}</button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('pre')\"><span style=\"font-family:monospace;\">{{ i18n.codeLabel }}</span></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h1')\"><h1 style=\"display:inline; margin:0;\">{{ i18n.h1Label }}</h1></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h2')\"><h2 style=\"display:inline; margin:0;\">{{ i18n.h2Label }}</h2></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h3')\"><h3 style=\"display:inline; margin:0;\">{{ i18n.h3Label }}</h3></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h4')\"><h4 style=\"display:inline; margin:0;\">{{ i18n.h4Label }}</h4></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h5')\"><h5 style=\"display:inline; margin:0;\">{{ i18n.h5Label }}</h5></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h6')\"><h6 style=\"display:inline; margin:0;\">{{ i18n.h6Label }}</h6></button>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Spacing menu -->\r\n @if (cfg.showSpacing) {\r\n <button mat-icon-button [matMenuTriggerFor]=\"spacingMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.spacingTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.spacingTitle }}\">\r\n <mat-icon fontIcon=\"space_bar\"></mat-icon>\r\n </button>\r\n <mat-menu #spacingMenu=\"matMenu\">\r\n <button mat-menu-item [matMenuTriggerFor]=\"marginMenu\">\r\n <mat-icon fontIcon=\"vertical_align_center\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.marginTitle }}</span>\r\n </button>\r\n <button mat-menu-item [matMenuTriggerFor]=\"paddingMenu\">\r\n <mat-icon fontIcon=\"crop_square\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.paddingTitle }}</span>\r\n </button>\r\n </mat-menu>\r\n\r\n <mat-menu #marginMenu=\"matMenu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <div style=\"padding:.5rem .75rem; font-weight:600; opacity:.8\">{{ i18n.marginTitle }}</div>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:0})\">0</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:4})\">4px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:8})\">8px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:12})\">12px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'vertical',value:16})\">Vert 16px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'horizontal',value:16})\">Horiz 16px</button>\r\n </mat-menu>\r\n\r\n <mat-menu #paddingMenu=\"matMenu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <div style=\"padding:.5rem .75rem; font-weight:600; opacity:.8\">{{ i18n.paddingTitle }}</div>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:0})\">0</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:4})\">4px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:8})\">8px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:12})\">12px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'vertical',value:16})\">Vert 16px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'horizontal',value:16})\">Horiz 16px</button>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Colors: keep native color input but wrap with mat-menu trigger for layout -->\r\n @if (cfg.showColor) {\r\n <button mat-icon-button [matMenuTriggerFor]=\"colorMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.colorsTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.colorsTitle }}\">\r\n <mat-icon fontIcon=\"palette\"></mat-icon>\r\n </button>\r\n <mat-menu #colorMenu=\"matMenu\">\r\n <div style=\"padding:.5rem .75rem; display:flex; gap:.75rem; align-items:center;\">\r\n <label class=\"stackch_rte__color\" (mousedown)=\"$event.stopPropagation(); onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\">\r\n <input type=\"color\" (mousedown)=\"onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\" (focus)=\"saveSelectionRequest.emit()\" (input)=\"applyColor.emit($any($event.target).value)\" (change)=\"applyColor.emit($any($event.target).value)\" [disabled]=\"disabled\" [title]=\"i18n.textColorTitle\" />\r\n A\r\n </label>\r\n <label class=\"stackch_rte__color\" [title]=\"i18n.highlightTitle\" (mousedown)=\"$event.stopPropagation(); onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\">\r\n <input type=\"color\" (mousedown)=\"onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\" (focus)=\"saveSelectionRequest.emit()\" (input)=\"applyHighlight.emit($any($event.target).value)\" (change)=\"applyHighlight.emit($any($event.target).value)\" [disabled]=\"disabled\" />\r\n \u29C9\r\n </label>\r\n </div>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Style buttons -->\r\n @if (cfg.showBold) {\r\n <button mat-icon-button [color]=\"isBoldActive ? 'primary' : undefined\" (mousedown)=\"onToolbarMouseDown($event)\" (click)=\"toggleBold.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.boldTitle\" aria-pressed=\"{{isBoldActive}}\">\r\n <mat-icon fontIcon=\"format_bold\"></mat-icon>\r\n </button>\r\n }\r\n @if (cfg.showItalic) {\r\n <button mat-icon-button [color]=\"isItalicActive ? 'primary' : undefined\" (mousedown)=\"onToolbarMouseDown($event)\" (click)=\"toggleItalic.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.italicTitle\" aria-pressed=\"{{isItalicActive}}\">\r\n <mat-icon fontIcon=\"format_italic\"></mat-icon>\r\n </button>\r\n }\r\n @if (cfg.showUnderline) {\r\n <button mat-icon-button [color]=\"isUnderlineActive ? 'primary' : undefined\" (mousedown)=\"onToolbarMouseDown($event)\" (click)=\"toggleUnderline.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.underlineTitle\" aria-pressed=\"{{isUnderlineActive}}\">\r\n <mat-icon fontIcon=\"format_underlined\"></mat-icon>\r\n </button>\r\n }\r\n <span class=\"stackch_rte__sep\"></span>\r\n\r\n @if (cfg.showLists) {\r\n <button mat-button [matMenuTriggerFor]=\"listMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.listsTitle\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <mat-icon fontIcon=\"format_list_bulleted\"></mat-icon>\r\n </button>\r\n <mat-menu #listMenu=\"matMenu\">\r\n <button mat-menu-item (click)=\"pickList.emit('ul')\">{{ i18n.bulletListTitle }}</button>\r\n <button mat-menu-item (click)=\"pickList.emit('ol')\">{{ i18n.numberedListTitle }}</button>\r\n </mat-menu>\r\n }\r\n <span class=\"stackch_rte__sep\"></span>\r\n\r\n @if (cfg.showAlign) {\r\n <button mat-button [matMenuTriggerFor]=\"alignMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.alignTitle\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <mat-icon fontIcon=\"format_align_left\"></mat-icon>\r\n </button>\r\n <mat-menu #alignMenu=\"matMenu\">\r\n <button mat-menu-item (click)=\"pickAlign.emit('left')\"><mat-icon fontIcon=\"format_align_left\"></mat-icon> {{ i18n.alignLeftTitle }}</button>\r\n <button mat-menu-item (click)=\"pickAlign.emit('center')\"><mat-icon fontIcon=\"format_align_center\"></mat-icon> {{ i18n.alignCenterTitle }}</button>\r\n <button mat-menu-item (click)=\"pickAlign.emit('right')\"><mat-icon fontIcon=\"format_align_right\"></mat-icon> {{ i18n.alignRightTitle }}</button>\r\n <button mat-menu-item (click)=\"pickAlign.emit('justify')\"><mat-icon fontIcon=\"format_align_justify\"></mat-icon> {{ i18n.alignJustifyTitle }}</button>\r\n </mat-menu>\r\n }\r\n <span class=\"stackch_rte__sep\"></span>\r\n\r\n @if (cfg.showLink) {\r\n <button mat-icon-button (click)=\"insertLink.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.linkTitle\" aria-label=\"{{i18n.linkTitle}}\">\r\n <mat-icon fontIcon=\"link\"></mat-icon>\r\n </button>\r\n }\r\n @if (cfg.showRemoveFormat) {\r\n <button mat-icon-button (click)=\"removeFormat.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.removeFormatTitle\" aria-label=\"{{i18n.removeFormatTitle}}\">\r\n <mat-icon fontIcon=\"format_clear\"></mat-icon>\r\n </button>\r\n }\r\n</div>\r\n", styles: [".stackch_rte{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,Arial,sans-serif}.stackch_rte--disabled{opacity:.7;pointer-events:none}.stackch_rte__toolbar{display:flex;flex-wrap:wrap;gap:.25rem;align-items:center;border:1px solid #d0d7de;border-bottom:none;background:#f6f8fa;padding:.25rem;border-radius:6px 6px 0 0}.stackch_rte__btn{appearance:none;border:1px solid #d0d7de;background:#fff;padding:.25rem .5rem;border-radius:4px;cursor:pointer;transition:background .12s ease,border-color .12s ease,color .12s ease;outline:none}.stackch_rte__btn:hover{background:#f3f4f6}.stackch_rte__btn:focus{outline:none;box-shadow:none}.stackch_rte__btn:focus-visible{outline:2px solid rgba(9,105,218,.2)}.stackch_rte__btn.is-active{background:#e7f3ff;border-color:#0969da;color:#084298}.stackch_rte__select{border:1px solid #d0d7de;border-radius:4px;padding:.2rem .4rem;background:#fff}.stackch_rte__color{display:inline-flex;align-items:center;gap:.25rem;border:1px solid #d0d7de;padding:0 .4rem;border-radius:4px;background:#fff}.stackch_rte__color>input{inline-size:1.75rem;block-size:1.5rem;padding:0;border:none;background:none}.stackch_rte__sep{width:1px;height:1.25rem;background:#d0d7de;margin:0 .125rem}.stackch_rte__editor{border:1px solid #d0d7de;border-radius:0 0 6px 6px;padding:.5rem;background:#fff;overflow:auto}.stackch_rte__editor:empty:before{content:attr(data-placeholder);color:#97a1ad;pointer-events:none}.stackch_rte__editor:focus{outline:none;box-shadow:inset 0 0 0 1px #0969da;border-color:#0969da}.stackch_rte__dropdown{position:relative;display:inline-block}.stackch_rte__menu{position:absolute;top:100%;left:0;z-index:2;background:#fff;border:1px solid #d0d7de;border-radius:6px;box-shadow:0 4px 12px #00000014;padding:.25rem;min-width:220px;max-height:220px;overflow:auto}.stackch_rte__menu-item{display:block;width:100%;text-align:left;background:#fff;border:none;padding:.375rem .5rem;border-radius:4px;cursor:pointer}.stackch_rte__menu-item:hover{background:#f3f4f6}.stackch_rte__menu--row{display:flex;flex-direction:row;gap:.25rem;align-items:center;min-width:auto;max-height:none;flex-wrap:nowrap}.stackch_rte__menu--row .stackch_rte__menu-item{display:inline-flex;width:auto;text-align:center;justify-content:center;align-items:center;padding:.3rem .45rem;white-space:nowrap;flex:0 0 auto;word-break:keep-all;overflow-wrap:normal}.stackch_rte__menu--row .stackch_rte__menu-item[style*=\"width:24px\"]{padding:0;width:24px;height:24px}.stackch_rte__menu--grid{display:grid;grid-template-columns:1fr 1fr;gap:.5rem 1rem;min-width:420px}.stackch_rte__menu-section{display:flex;flex-direction:column;gap:.25rem}.stackch_rte__menu-title{font-size:12px;color:#57606a;padding:.25rem;text-transform:uppercase;letter-spacing:.04em}\n"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "ngmodule", type: MatButtonModule }, { kind: "component", type: i1.MatButton, selector: " button[matButton], a[matButton], button[mat-button], button[mat-raised-button], button[mat-flat-button], button[mat-stroked-button], a[mat-button], a[mat-raised-button], a[mat-flat-button], a[mat-stroked-button] ", inputs: ["matButton"], exportAs: ["matButton", "matAnchor"] }, { kind: "component", type: i1.MatIconButton, selector: "button[mat-icon-button], a[mat-icon-button], button[matIconButton], a[matIconButton]", exportAs: ["matButton", "matAnchor"] }, { kind: "ngmodule", type: MatIconModule }, { kind: "component", type: i2.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }, { kind: "ngmodule", type: MatMenuModule }, { kind: "component", type: i3.MatMenu, selector: "mat-menu", inputs: ["backdropClass", "aria-label", "aria-labelledby", "aria-describedby", "xPosition", "yPosition", "overlapTrigger", "hasBackdrop", "class", "classList"], outputs: ["closed", "close"], exportAs: ["matMenu"] }, { kind: "component", type: i3.MatMenuItem, selector: "[mat-menu-item]", inputs: ["role", "disabled", "disableRipple"], exportAs: ["matMenuItem"] }, { kind: "directive", type: i3.MatMenuTrigger, selector: "[mat-menu-trigger-for], [matMenuTriggerFor]", inputs: ["mat-menu-trigger-for", "matMenuTriggerFor", "matMenuTriggerData", "matMenuTriggerRestoreFocus"], outputs: ["menuOpened", "onMenuOpen", "menuClosed", "onMenuClose"], exportAs: ["matMenuTrigger"] }, { kind: "ngmodule", type: MatTooltipModule }, { kind: "directive", type: i4.MatTooltip, selector: "[matTooltip]", inputs: ["matTooltipPosition", "matTooltipPositionAtOrigin", "matTooltipDisabled", "matTooltipShowDelay", "matTooltipHideDelay", "matTooltipTouchGestures", "matTooltip", "matTooltipClass"], exportAs: ["matTooltip"] }, { kind: "ngmodule", type: MatFormFieldModule }, { kind: "ngmodule", type: MatSelectModule }] });
67
+ }
68
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: StackchRichtextEditorMaterialToolbar, decorators: [{
69
+ type: Component,
70
+ args: [{ selector: 'stackch-richtext-editor-material-toolbar', standalone: true, imports: [
71
+ CommonModule,
72
+ MatButtonModule,
73
+ MatIconModule,
74
+ MatMenuModule,
75
+ MatTooltipModule,
76
+ MatFormFieldModule,
77
+ MatSelectModule,
78
+ ], template: "<div class=\"stackch_rte__toolbar\">\r\n <!-- Undo / Redo -->\r\n @if (cfg.showUndoRedo) {\r\n <button mat-icon-button (click)=\"undo.emit()\" [disabled]=\"disabled || !canUndo\" [matTooltip]=\"i18n.undoTitle\" aria-label=\"{{i18n.undoTitle}}\">\r\n <mat-icon fontIcon=\"undo\"></mat-icon>\r\n </button>\r\n <button mat-icon-button (click)=\"redo.emit()\" [disabled]=\"disabled || !canRedo\" [matTooltip]=\"i18n.redoTitle\" aria-label=\"{{i18n.redoTitle}}\">\r\n <mat-icon fontIcon=\"redo\"></mat-icon>\r\n </button>\r\n <span class=\"stackch_rte__sep\"></span>\r\n }\r\n\r\n <!-- Font & Size: single icon opens a menu with two submenus (Fonts, Sizes) -->\r\n @if (cfg.showFontPanel) {\r\n <button mat-icon-button [matMenuTriggerFor]=\"fontPanelMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.fontPanelTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.fontPanelTitle }}\">\r\n <mat-icon fontIcon=\"text_fields\"></mat-icon>\r\n </button>\r\n <mat-menu #fontPanelMenu=\"matMenu\">\r\n <button mat-menu-item [matMenuTriggerFor]=\"fontMenu\">\r\n <mat-icon fontIcon=\"text_fields\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.fontSectionTitle }}</span>\r\n </button>\r\n <button mat-menu-item [matMenuTriggerFor]=\"sizeMenu\">\r\n <mat-icon fontIcon=\"format_size\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.sizeSectionTitle }}</span>\r\n </button>\r\n </mat-menu>\r\n\r\n <mat-menu #fontMenu=\"matMenu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n @for (f of fonts; track f) {\r\n <button mat-menu-item (click)=\"pickFont.emit(f)\"><span [style.fontFamily]=\"f\">{{ f }}</span></button>\r\n }\r\n </mat-menu>\r\n\r\n <mat-menu #sizeMenu=\"matMenu\" class=\"stackch_rte__size-menu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <div style=\"display:grid; grid-template-columns: repeat(3, auto); gap:.25rem .5rem; padding:.25rem .5rem;\">\r\n @for (s of fontSizes; track s) {\r\n <button mat-menu-item style=\"min-width:auto; padding:0 .5rem; line-height:28px; height:28px;\" (click)=\"pickFontSize.emit(s)\">{{ s }}px</button>\r\n }\r\n </div>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Headings menu -->\r\n @if (cfg.showHeading) {\r\n <button mat-button [matMenuTriggerFor]=\"headingMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.headingTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.headingTitle }}\">\r\n <mat-icon fontIcon=\"title\"></mat-icon>\r\n </button>\r\n <mat-menu #headingMenu=\"matMenu\">\r\n <button mat-menu-item (click)=\"pickHeading.emit('p')\">{{ i18n.paragraphLabel }}</button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('pre')\"><span style=\"font-family:monospace;\">{{ i18n.codeLabel }}</span></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h1')\"><h1 style=\"display:inline; margin:0;\">{{ i18n.h1Label }}</h1></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h2')\"><h2 style=\"display:inline; margin:0;\">{{ i18n.h2Label }}</h2></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h3')\"><h3 style=\"display:inline; margin:0;\">{{ i18n.h3Label }}</h3></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h4')\"><h4 style=\"display:inline; margin:0;\">{{ i18n.h4Label }}</h4></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h5')\"><h5 style=\"display:inline; margin:0;\">{{ i18n.h5Label }}</h5></button>\r\n <button mat-menu-item (click)=\"pickHeading.emit('h6')\"><h6 style=\"display:inline; margin:0;\">{{ i18n.h6Label }}</h6></button>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Spacing menu -->\r\n @if (cfg.showSpacing) {\r\n <button mat-icon-button [matMenuTriggerFor]=\"spacingMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.spacingTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.spacingTitle }}\">\r\n <mat-icon fontIcon=\"space_bar\"></mat-icon>\r\n </button>\r\n <mat-menu #spacingMenu=\"matMenu\">\r\n <button mat-menu-item [matMenuTriggerFor]=\"marginMenu\">\r\n <mat-icon fontIcon=\"vertical_align_center\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.marginTitle }}</span>\r\n </button>\r\n <button mat-menu-item [matMenuTriggerFor]=\"paddingMenu\">\r\n <mat-icon fontIcon=\"crop_square\"></mat-icon>\r\n <span style=\"margin-left:.5rem;\">{{ i18n.paddingTitle }}</span>\r\n </button>\r\n </mat-menu>\r\n\r\n <mat-menu #marginMenu=\"matMenu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <div style=\"padding:.5rem .75rem; font-weight:600; opacity:.8\">{{ i18n.marginTitle }}</div>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:0})\">0</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:4})\">4px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:8})\">8px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'all',value:12})\">12px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'vertical',value:16})\">Vert 16px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'margin',target:'horizontal',value:16})\">Horiz 16px</button>\r\n </mat-menu>\r\n\r\n <mat-menu #paddingMenu=\"matMenu\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <div style=\"padding:.5rem .75rem; font-weight:600; opacity:.8\">{{ i18n.paddingTitle }}</div>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:0})\">0</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:4})\">4px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:8})\">8px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'all',value:12})\">12px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'vertical',value:16})\">Vert 16px</button>\r\n <button mat-menu-item (click)=\"pickSpacing.emit({kind:'padding',target:'horizontal',value:16})\">Horiz 16px</button>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Colors: keep native color input but wrap with mat-menu trigger for layout -->\r\n @if (cfg.showColor) {\r\n <button mat-icon-button [matMenuTriggerFor]=\"colorMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.colorsTitle\" (menuOpened)=\"saveSelectionRequest.emit()\" aria-label=\"{{ i18n.colorsTitle }}\">\r\n <mat-icon fontIcon=\"palette\"></mat-icon>\r\n </button>\r\n <mat-menu #colorMenu=\"matMenu\">\r\n <div style=\"padding:.5rem .75rem; display:flex; gap:.75rem; align-items:center;\">\r\n <label class=\"stackch_rte__color\" (mousedown)=\"$event.stopPropagation(); onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\">\r\n <input type=\"color\" (mousedown)=\"onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\" (focus)=\"saveSelectionRequest.emit()\" (input)=\"applyColor.emit($any($event.target).value)\" (change)=\"applyColor.emit($any($event.target).value)\" [disabled]=\"disabled\" [title]=\"i18n.textColorTitle\" />\r\n A\r\n </label>\r\n <label class=\"stackch_rte__color\" [title]=\"i18n.highlightTitle\" (mousedown)=\"$event.stopPropagation(); onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\">\r\n <input type=\"color\" (mousedown)=\"onColorPointerDown($event)\" (click)=\"$event.stopPropagation()\" (pointerdown)=\"onColorPointerDown($event)\" (touchstart)=\"onColorPointerDown($event)\" (focus)=\"saveSelectionRequest.emit()\" (input)=\"applyHighlight.emit($any($event.target).value)\" (change)=\"applyHighlight.emit($any($event.target).value)\" [disabled]=\"disabled\" />\r\n \u29C9\r\n </label>\r\n </div>\r\n </mat-menu>\r\n }\r\n\r\n <!-- Style buttons -->\r\n @if (cfg.showBold) {\r\n <button mat-icon-button [color]=\"isBoldActive ? 'primary' : undefined\" (mousedown)=\"onToolbarMouseDown($event)\" (click)=\"toggleBold.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.boldTitle\" aria-pressed=\"{{isBoldActive}}\">\r\n <mat-icon fontIcon=\"format_bold\"></mat-icon>\r\n </button>\r\n }\r\n @if (cfg.showItalic) {\r\n <button mat-icon-button [color]=\"isItalicActive ? 'primary' : undefined\" (mousedown)=\"onToolbarMouseDown($event)\" (click)=\"toggleItalic.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.italicTitle\" aria-pressed=\"{{isItalicActive}}\">\r\n <mat-icon fontIcon=\"format_italic\"></mat-icon>\r\n </button>\r\n }\r\n @if (cfg.showUnderline) {\r\n <button mat-icon-button [color]=\"isUnderlineActive ? 'primary' : undefined\" (mousedown)=\"onToolbarMouseDown($event)\" (click)=\"toggleUnderline.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.underlineTitle\" aria-pressed=\"{{isUnderlineActive}}\">\r\n <mat-icon fontIcon=\"format_underlined\"></mat-icon>\r\n </button>\r\n }\r\n <span class=\"stackch_rte__sep\"></span>\r\n\r\n @if (cfg.showLists) {\r\n <button mat-button [matMenuTriggerFor]=\"listMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.listsTitle\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <mat-icon fontIcon=\"format_list_bulleted\"></mat-icon>\r\n </button>\r\n <mat-menu #listMenu=\"matMenu\">\r\n <button mat-menu-item (click)=\"pickList.emit('ul')\">{{ i18n.bulletListTitle }}</button>\r\n <button mat-menu-item (click)=\"pickList.emit('ol')\">{{ i18n.numberedListTitle }}</button>\r\n </mat-menu>\r\n }\r\n <span class=\"stackch_rte__sep\"></span>\r\n\r\n @if (cfg.showAlign) {\r\n <button mat-button [matMenuTriggerFor]=\"alignMenu\" [disabled]=\"disabled\" [matTooltip]=\"i18n.alignTitle\" (menuOpened)=\"saveSelectionRequest.emit()\">\r\n <mat-icon fontIcon=\"format_align_left\"></mat-icon>\r\n </button>\r\n <mat-menu #alignMenu=\"matMenu\">\r\n <button mat-menu-item (click)=\"pickAlign.emit('left')\"><mat-icon fontIcon=\"format_align_left\"></mat-icon> {{ i18n.alignLeftTitle }}</button>\r\n <button mat-menu-item (click)=\"pickAlign.emit('center')\"><mat-icon fontIcon=\"format_align_center\"></mat-icon> {{ i18n.alignCenterTitle }}</button>\r\n <button mat-menu-item (click)=\"pickAlign.emit('right')\"><mat-icon fontIcon=\"format_align_right\"></mat-icon> {{ i18n.alignRightTitle }}</button>\r\n <button mat-menu-item (click)=\"pickAlign.emit('justify')\"><mat-icon fontIcon=\"format_align_justify\"></mat-icon> {{ i18n.alignJustifyTitle }}</button>\r\n </mat-menu>\r\n }\r\n <span class=\"stackch_rte__sep\"></span>\r\n\r\n @if (cfg.showLink) {\r\n <button mat-icon-button (click)=\"insertLink.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.linkTitle\" aria-label=\"{{i18n.linkTitle}}\">\r\n <mat-icon fontIcon=\"link\"></mat-icon>\r\n </button>\r\n }\r\n @if (cfg.showRemoveFormat) {\r\n <button mat-icon-button (click)=\"removeFormat.emit()\" [disabled]=\"disabled\" [matTooltip]=\"i18n.removeFormatTitle\" aria-label=\"{{i18n.removeFormatTitle}}\">\r\n <mat-icon fontIcon=\"format_clear\"></mat-icon>\r\n </button>\r\n }\r\n</div>\r\n", styles: [".stackch_rte{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,Arial,sans-serif}.stackch_rte--disabled{opacity:.7;pointer-events:none}.stackch_rte__toolbar{display:flex;flex-wrap:wrap;gap:.25rem;align-items:center;border:1px solid #d0d7de;border-bottom:none;background:#f6f8fa;padding:.25rem;border-radius:6px 6px 0 0}.stackch_rte__btn{appearance:none;border:1px solid #d0d7de;background:#fff;padding:.25rem .5rem;border-radius:4px;cursor:pointer;transition:background .12s ease,border-color .12s ease,color .12s ease;outline:none}.stackch_rte__btn:hover{background:#f3f4f6}.stackch_rte__btn:focus{outline:none;box-shadow:none}.stackch_rte__btn:focus-visible{outline:2px solid rgba(9,105,218,.2)}.stackch_rte__btn.is-active{background:#e7f3ff;border-color:#0969da;color:#084298}.stackch_rte__select{border:1px solid #d0d7de;border-radius:4px;padding:.2rem .4rem;background:#fff}.stackch_rte__color{display:inline-flex;align-items:center;gap:.25rem;border:1px solid #d0d7de;padding:0 .4rem;border-radius:4px;background:#fff}.stackch_rte__color>input{inline-size:1.75rem;block-size:1.5rem;padding:0;border:none;background:none}.stackch_rte__sep{width:1px;height:1.25rem;background:#d0d7de;margin:0 .125rem}.stackch_rte__editor{border:1px solid #d0d7de;border-radius:0 0 6px 6px;padding:.5rem;background:#fff;overflow:auto}.stackch_rte__editor:empty:before{content:attr(data-placeholder);color:#97a1ad;pointer-events:none}.stackch_rte__editor:focus{outline:none;box-shadow:inset 0 0 0 1px #0969da;border-color:#0969da}.stackch_rte__dropdown{position:relative;display:inline-block}.stackch_rte__menu{position:absolute;top:100%;left:0;z-index:2;background:#fff;border:1px solid #d0d7de;border-radius:6px;box-shadow:0 4px 12px #00000014;padding:.25rem;min-width:220px;max-height:220px;overflow:auto}.stackch_rte__menu-item{display:block;width:100%;text-align:left;background:#fff;border:none;padding:.375rem .5rem;border-radius:4px;cursor:pointer}.stackch_rte__menu-item:hover{background:#f3f4f6}.stackch_rte__menu--row{display:flex;flex-direction:row;gap:.25rem;align-items:center;min-width:auto;max-height:none;flex-wrap:nowrap}.stackch_rte__menu--row .stackch_rte__menu-item{display:inline-flex;width:auto;text-align:center;justify-content:center;align-items:center;padding:.3rem .45rem;white-space:nowrap;flex:0 0 auto;word-break:keep-all;overflow-wrap:normal}.stackch_rte__menu--row .stackch_rte__menu-item[style*=\"width:24px\"]{padding:0;width:24px;height:24px}.stackch_rte__menu--grid{display:grid;grid-template-columns:1fr 1fr;gap:.5rem 1rem;min-width:420px}.stackch_rte__menu-section{display:flex;flex-direction:column;gap:.25rem}.stackch_rte__menu-title{font-size:12px;color:#57606a;padding:.25rem;text-transform:uppercase;letter-spacing:.04em}\n"] }]
79
+ }], propDecorators: { cfg: [{
80
+ type: Input
81
+ }], i18n: [{
82
+ type: Input
83
+ }], disabled: [{
84
+ type: Input
85
+ }], fonts: [{
86
+ type: Input
87
+ }], fontSizes: [{
88
+ type: Input
89
+ }], isBoldActive: [{
90
+ type: Input
91
+ }], isItalicActive: [{
92
+ type: Input
93
+ }], isUnderlineActive: [{
94
+ type: Input
95
+ }], canUndo: [{
96
+ type: Input
97
+ }], canRedo: [{
98
+ type: Input
99
+ }], uiState: [{
100
+ type: Input
101
+ }], undo: [{
102
+ type: Output
103
+ }], redo: [{
104
+ type: Output
105
+ }], toggleFontPanel: [{
106
+ type: Output
107
+ }], toggleHeadingMenu: [{
108
+ type: Output
109
+ }], toggleSpacingMenu: [{
110
+ type: Output
111
+ }], toggleAlignMenu: [{
112
+ type: Output
113
+ }], toggleColorMenu: [{
114
+ type: Output
115
+ }], toggleListMenu: [{
116
+ type: Output
117
+ }], pickFont: [{
118
+ type: Output
119
+ }], pickFontSize: [{
120
+ type: Output
121
+ }], pickAlign: [{
122
+ type: Output
123
+ }], pickList: [{
124
+ type: Output
125
+ }], pickHeading: [{
126
+ type: Output
127
+ }], pickSpacing: [{
128
+ type: Output
129
+ }], applyColor: [{
130
+ type: Output
131
+ }], applyHighlight: [{
132
+ type: Output
133
+ }], toggleBold: [{
134
+ type: Output
135
+ }], toggleItalic: [{
136
+ type: Output
137
+ }], toggleUnderline: [{
138
+ type: Output
139
+ }], insertLink: [{
140
+ type: Output
141
+ }], removeFormat: [{
142
+ type: Output
143
+ }], saveSelectionRequest: [{
144
+ type: Output
145
+ }] } });
146
+
147
+ class StackchRichtextEditorConfig {
148
+ // Sichtbarkeit einzelner Toolbar-Elemente (Default: an)
149
+ showUndoRedo = true;
150
+ showFontPanel = true;
151
+ showHeading = true;
152
+ showSpacing = true;
153
+ showColor = true;
154
+ showBold = true;
155
+ showItalic = true;
156
+ showUnderline = true;
157
+ showLists = true;
158
+ showAlign = true;
159
+ showLink = true;
160
+ showRemoveFormat = true;
161
+ // i18n overrides (partial), default is English
162
+ i18n;
163
+ }
164
+ class StackchRichtextEditorI18n {
165
+ // Generic
166
+ placeholder = 'Write…';
167
+ // Toolbar titles
168
+ undoTitle = 'Undo (Ctrl+Z)';
169
+ redoTitle = 'Redo (Ctrl+Y)';
170
+ fontPanelTitle = 'Font & Size';
171
+ fontSectionTitle = 'Font';
172
+ sizeSectionTitle = 'Size';
173
+ headingTitle = 'Heading';
174
+ spacingTitle = 'Spacing (Margin/Padding)';
175
+ marginTitle = 'Margin';
176
+ paddingTitle = 'Padding';
177
+ colorsTitle = 'Colors';
178
+ colorMenuTitle = 'Pick a color';
179
+ textColorTitle = 'Text color';
180
+ highlightTitle = 'Highlight';
181
+ boldTitle = 'Bold';
182
+ italicTitle = 'Italic';
183
+ underlineTitle = 'Underline';
184
+ listsTitle = 'Lists';
185
+ bulletListTitle = 'Bullet list';
186
+ numberedListTitle = 'Numbered list';
187
+ alignTitle = 'Alignment';
188
+ alignLeftTitle = 'Left';
189
+ alignCenterTitle = 'Center';
190
+ alignRightTitle = 'Right';
191
+ alignJustifyTitle = 'Justify';
192
+ linkTitle = 'Link';
193
+ removeFormatTitle = 'Remove format';
194
+ // Heading menu labels
195
+ paragraphLabel = 'Paragraph (P)';
196
+ codeLabel = 'Code (pre)';
197
+ h1Label = 'H1';
198
+ h2Label = 'H2';
199
+ h3Label = 'H3';
200
+ h4Label = 'H4';
201
+ h5Label = 'H5';
202
+ h6Label = 'H6';
203
+ }
204
+ // Predefined i18n bundles
205
+ // Consumers can import these and pass via config.i18n to localize the toolbar/labels.
206
+ const STACKCH_RTE_I18N_DE = {
207
+ // Generic
208
+ placeholder: 'Schreiben…',
209
+ // Toolbar titles
210
+ undoTitle: 'Rückgängig (Strg+Z)',
211
+ redoTitle: 'Wiederholen (Strg+Y)',
212
+ fontPanelTitle: 'Schrift & Größe',
213
+ fontSectionTitle: 'Schrift',
214
+ sizeSectionTitle: 'Größe',
215
+ headingTitle: 'Überschrift',
216
+ spacingTitle: 'Abstand (Außen/Innen)',
217
+ marginTitle: 'Außenabstand',
218
+ paddingTitle: 'Innenabstand',
219
+ colorsTitle: 'Farben',
220
+ colorMenuTitle: 'Farbe wählen',
221
+ textColorTitle: 'Textfarbe',
222
+ highlightTitle: 'Hervorheben',
223
+ boldTitle: 'Fett',
224
+ italicTitle: 'Kursiv',
225
+ underlineTitle: 'Unterstreichen',
226
+ listsTitle: 'Listen',
227
+ bulletListTitle: 'Aufzählung',
228
+ numberedListTitle: 'Nummeriert',
229
+ alignTitle: 'Ausrichtung',
230
+ alignLeftTitle: 'Links',
231
+ alignCenterTitle: 'Zentriert',
232
+ alignRightTitle: 'Rechts',
233
+ alignJustifyTitle: 'Blocksatz',
234
+ linkTitle: 'Link',
235
+ removeFormatTitle: 'Formatierung entfernen',
236
+ // Heading menu labels
237
+ paragraphLabel: 'Absatz (P)',
238
+ codeLabel: 'Code (pre)',
239
+ h1Label: 'H1',
240
+ h2Label: 'H2',
241
+ h3Label: 'H3',
242
+ h4Label: 'H4',
243
+ h5Label: 'H5',
244
+ h6Label: 'H6',
245
+ };
246
+ const STACKCH_RTE_I18N_FR = {
247
+ // Generic
248
+ placeholder: 'Écrire…',
249
+ // Toolbar titles
250
+ undoTitle: 'Annuler (Ctrl+Z)',
251
+ redoTitle: 'Rétablir (Ctrl+Y)',
252
+ fontPanelTitle: 'Police et taille',
253
+ fontSectionTitle: 'Police',
254
+ sizeSectionTitle: 'Taille',
255
+ headingTitle: 'Titre',
256
+ spacingTitle: 'Espacement (Marge/Remplissage)',
257
+ marginTitle: 'Marge',
258
+ paddingTitle: 'Remplissage',
259
+ colorsTitle: 'Couleurs',
260
+ colorMenuTitle: 'Choisir une couleur',
261
+ textColorTitle: 'Couleur du texte',
262
+ highlightTitle: 'Surligner',
263
+ boldTitle: 'Gras',
264
+ italicTitle: 'Italique',
265
+ underlineTitle: 'Souligné',
266
+ listsTitle: 'Listes',
267
+ bulletListTitle: 'Liste à puces',
268
+ numberedListTitle: 'Liste numérotée',
269
+ alignTitle: 'Alignement',
270
+ alignLeftTitle: 'Gauche',
271
+ alignCenterTitle: 'Centré',
272
+ alignRightTitle: 'Droite',
273
+ alignJustifyTitle: 'Justifié',
274
+ linkTitle: 'Lien',
275
+ removeFormatTitle: 'Effacer la mise en forme',
276
+ // Heading menu labels
277
+ paragraphLabel: 'Paragraphe (P)',
278
+ codeLabel: 'Code (pre)',
279
+ h1Label: 'H1',
280
+ h2Label: 'H2',
281
+ h3Label: 'H3',
282
+ h4Label: 'H4',
283
+ h5Label: 'H5',
284
+ h6Label: 'H6',
285
+ };
286
+ const STACKCH_RTE_I18N_IT = {
287
+ // Generic
288
+ placeholder: 'Scrivi…',
289
+ // Toolbar titles
290
+ undoTitle: 'Annulla (Ctrl+Z)',
291
+ redoTitle: 'Ripristina (Ctrl+Y)',
292
+ fontPanelTitle: 'Carattere e dimensione',
293
+ fontSectionTitle: 'Carattere',
294
+ sizeSectionTitle: 'Dimensione',
295
+ headingTitle: 'Titolo',
296
+ spacingTitle: 'Spaziatura (Margine/Riempimento)',
297
+ marginTitle: 'Margine',
298
+ paddingTitle: 'Riempimento',
299
+ colorsTitle: 'Colori',
300
+ colorMenuTitle: 'Scegli un colore',
301
+ textColorTitle: 'Colore del testo',
302
+ highlightTitle: 'Evidenzia',
303
+ boldTitle: 'Grassetto',
304
+ italicTitle: 'Corsivo',
305
+ underlineTitle: 'Sottolineato',
306
+ listsTitle: 'Elenchi',
307
+ bulletListTitle: 'Elenco puntato',
308
+ numberedListTitle: 'Elenco numerato',
309
+ alignTitle: 'Allineamento',
310
+ alignLeftTitle: 'Sinistra',
311
+ alignCenterTitle: 'Centrato',
312
+ alignRightTitle: 'Destra',
313
+ alignJustifyTitle: 'Giustificato',
314
+ linkTitle: 'Collegamento',
315
+ removeFormatTitle: 'Rimuovi formattazione',
316
+ // Heading menu labels
317
+ paragraphLabel: 'Paragrafo (P)',
318
+ codeLabel: 'Codice (pre)',
319
+ h1Label: 'H1',
320
+ h2Label: 'H2',
321
+ h3Label: 'H3',
322
+ h4Label: 'H4',
323
+ h5Label: 'H5',
324
+ h6Label: 'H6',
325
+ };
326
+ class StackchRichtextEditorMaterial {
327
+ cdr;
328
+ placeholder = '';
329
+ showToolbar = true;
330
+ fonts = [
331
+ 'Arial, Helvetica, sans-serif',
332
+ 'Georgia, serif',
333
+ 'Times New Roman, Times, serif',
334
+ 'Trebuchet MS, sans-serif',
335
+ 'Verdana, Geneva, sans-serif',
336
+ 'Courier New, Courier, monospace',
337
+ 'Monaco, monospace'
338
+ ];
339
+ fontSizes = [12, 14, 16, 18, 20, 24, 28, 32];
340
+ height;
341
+ minHeight = 160;
342
+ maxHeight;
343
+ set disabled(value) { this._disabled = value; }
344
+ get disabled() { return this._disabled; }
345
+ _disabled = false;
346
+ valueChange = new EventEmitter();
347
+ editorRef;
348
+ constructor(cdr) {
349
+ this.cdr = cdr;
350
+ }
351
+ // Toolbar-Konfiguration (default: alles an). Wir halten die übergebene Referenz
352
+ // und mergen Defaults im Getter, damit auch Eigenschaftsänderungen via ngModel sofort wirken.
353
+ config;
354
+ get cfg() {
355
+ return Object.assign(new StackchRichtextEditorConfig(), this.config || {});
356
+ }
357
+ get i18n() {
358
+ return Object.assign(new StackchRichtextEditorI18n(), this.cfg.i18n || {});
359
+ }
360
+ onChange = () => { };
361
+ onTouched = () => { };
362
+ savedRange = null;
363
+ // History (Undo/Redo)
364
+ history = [];
365
+ historyIndex = -1;
366
+ isRestoringHistory = false;
367
+ snapshotTimer = null;
368
+ // UI state for compact dropdowns / panel
369
+ showFontMenu = false;
370
+ showSizeMenu = false;
371
+ showFontPanel = false;
372
+ showHeadingMenu = false;
373
+ showSpacingMenu = false;
374
+ showAlignMenu = false;
375
+ showColorMenu = false;
376
+ showListMenu = false;
377
+ // Inline state flags for active buttons
378
+ isBoldActive = false;
379
+ isItalicActive = false;
380
+ isUnderlineActive = false;
381
+ // Selection helpers
382
+ saveSelection() {
383
+ const sel = window.getSelection();
384
+ if (!sel || sel.rangeCount === 0)
385
+ return;
386
+ const range = sel.getRangeAt(0);
387
+ if (!this.isRangeInEditor(range))
388
+ return;
389
+ this.savedRange = range.cloneRange();
390
+ this.updateInlineStates();
391
+ }
392
+ // Prevent toolbar buttons from stealing focus from the editor while preserving selection
393
+ onToolbarMouseDown(evt) {
394
+ // Keep focus on the editor to avoid persistent button focus outlines
395
+ evt.preventDefault();
396
+ evt.stopPropagation();
397
+ this.saveSelection();
398
+ }
399
+ // Beim Öffnen des Color-Pickers: Selektion sichern
400
+ onColorPointerDown(_evt) {
401
+ this.saveSelection();
402
+ }
403
+ restoreSelection() {
404
+ const sel = window.getSelection();
405
+ if (!sel || !this.savedRange)
406
+ return;
407
+ if (!this.isRangeInEditor(this.savedRange))
408
+ return;
409
+ sel.removeAllRanges();
410
+ sel.addRange(this.savedRange);
411
+ }
412
+ isRangeInEditor(range) {
413
+ const editor = this.editorRef?.nativeElement;
414
+ if (!editor)
415
+ return false;
416
+ const container = range.commonAncestorContainer;
417
+ const node = container.nodeType === Node.ELEMENT_NODE ? container : container.parentElement;
418
+ return !!node && editor.contains(node);
419
+ }
420
+ // Update active-state on selection changes inside the editor
421
+ onSelectionChange() {
422
+ const sel = window.getSelection();
423
+ if (!sel || sel.rangeCount === 0)
424
+ return;
425
+ const range = sel.getRangeAt(0);
426
+ if (!this.isRangeInEditor(range))
427
+ return;
428
+ this.updateInlineStates();
429
+ }
430
+ // Close dropdowns when clicking anywhere in the document
431
+ closeMenus() {
432
+ this.showFontMenu = false;
433
+ this.showSizeMenu = false;
434
+ this.showFontPanel = false;
435
+ this.showHeadingMenu = false;
436
+ this.showSpacingMenu = false;
437
+ this.showAlignMenu = false;
438
+ this.showColorMenu = false;
439
+ this.showListMenu = false;
440
+ }
441
+ // Keyboard: Undo/Redo
442
+ onKeydown(evt) {
443
+ const isMac = navigator.platform.toLowerCase().includes('mac');
444
+ const mod = isMac ? evt.metaKey : evt.ctrlKey;
445
+ if (mod && !evt.shiftKey && (evt.key === 'z' || evt.key === 'Z')) {
446
+ evt.preventDefault();
447
+ this.undo();
448
+ }
449
+ else if (mod && (evt.key === 'y' || evt.key === 'Y' || (evt.shiftKey && (evt.key === 'z' || evt.key === 'Z')))) {
450
+ evt.preventDefault();
451
+ this.redo();
452
+ }
453
+ }
454
+ toggleFontMenu(evt) {
455
+ this.saveSelection();
456
+ this.showFontMenu = !this.showFontMenu;
457
+ if (this.showFontMenu)
458
+ this.showSizeMenu = false;
459
+ evt.stopPropagation();
460
+ }
461
+ toggleSizeMenu(evt) {
462
+ this.saveSelection();
463
+ this.showSizeMenu = !this.showSizeMenu;
464
+ if (this.showSizeMenu)
465
+ this.showFontMenu = false;
466
+ evt.stopPropagation();
467
+ }
468
+ toggleFontPanel(evt) {
469
+ this.saveSelection();
470
+ this.showFontPanel = !this.showFontPanel;
471
+ // close others if open
472
+ if (this.showFontPanel) {
473
+ this.showFontMenu = false;
474
+ this.showSizeMenu = false;
475
+ this.showHeadingMenu = false;
476
+ this.showSpacingMenu = false;
477
+ this.showAlignMenu = false;
478
+ this.showColorMenu = false;
479
+ }
480
+ evt.stopPropagation();
481
+ }
482
+ toggleHeadingMenu(evt) {
483
+ this.saveSelection();
484
+ this.showHeadingMenu = !this.showHeadingMenu;
485
+ if (this.showHeadingMenu) {
486
+ this.showFontMenu = false;
487
+ this.showSizeMenu = false;
488
+ this.showFontPanel = false;
489
+ this.showSpacingMenu = false;
490
+ this.showAlignMenu = false;
491
+ this.showColorMenu = false;
492
+ }
493
+ evt.stopPropagation();
494
+ }
495
+ toggleSpacingMenu(evt) {
496
+ this.saveSelection();
497
+ this.showSpacingMenu = !this.showSpacingMenu;
498
+ if (this.showSpacingMenu) {
499
+ this.showFontPanel = false;
500
+ this.showHeadingMenu = false;
501
+ this.showAlignMenu = false;
502
+ this.showColorMenu = false;
503
+ }
504
+ evt.stopPropagation();
505
+ }
506
+ toggleAlignMenu(evt) {
507
+ this.saveSelection();
508
+ this.showAlignMenu = !this.showAlignMenu;
509
+ if (this.showAlignMenu) {
510
+ this.showFontMenu = false;
511
+ this.showSizeMenu = false;
512
+ this.showFontPanel = false;
513
+ this.showHeadingMenu = false;
514
+ this.showSpacingMenu = false;
515
+ this.showColorMenu = false;
516
+ this.showListMenu = false;
517
+ }
518
+ evt.stopPropagation();
519
+ }
520
+ toggleColorMenu(evt) {
521
+ this.saveSelection();
522
+ this.showColorMenu = !this.showColorMenu;
523
+ if (this.showColorMenu) {
524
+ this.showFontMenu = false;
525
+ this.showSizeMenu = false;
526
+ this.showFontPanel = false;
527
+ this.showHeadingMenu = false;
528
+ this.showSpacingMenu = false;
529
+ this.showAlignMenu = false;
530
+ this.showListMenu = false;
531
+ }
532
+ evt.stopPropagation();
533
+ }
534
+ toggleListMenu(evt) {
535
+ this.saveSelection();
536
+ this.showListMenu = !this.showListMenu;
537
+ if (this.showListMenu) {
538
+ this.showFontMenu = false;
539
+ this.showSizeMenu = false;
540
+ this.showFontPanel = false;
541
+ this.showHeadingMenu = false;
542
+ this.showSpacingMenu = false;
543
+ this.showAlignMenu = false;
544
+ this.showColorMenu = false;
545
+ }
546
+ evt.stopPropagation();
547
+ }
548
+ onPickAlign(where) {
549
+ this.focusEditor();
550
+ this.restoreSelection();
551
+ this.alignBlocks(where);
552
+ this.showAlignMenu = false;
553
+ this.emitValue();
554
+ this.takeSnapshot('align');
555
+ }
556
+ onPickFont(font) {
557
+ if (!font)
558
+ return;
559
+ this.focusEditor();
560
+ this.restoreSelection();
561
+ this.applyInlineStyle('fontFamily', font);
562
+ this.showFontMenu = false;
563
+ }
564
+ onPickFontSize(size) {
565
+ if (!Number.isFinite(size))
566
+ return;
567
+ this.focusEditor();
568
+ this.restoreSelection();
569
+ this.applyInlineStyle('fontSize', `${size}px`);
570
+ this.showSizeMenu = false;
571
+ }
572
+ onPickList(kind) {
573
+ this.focusEditor();
574
+ this.restoreSelection();
575
+ this.toggleList(kind);
576
+ this.showListMenu = false;
577
+ this.emitValue();
578
+ this.takeSnapshot('list');
579
+ }
580
+ onPickHeading(tag) {
581
+ this.focusEditor();
582
+ this.restoreSelection();
583
+ this.setHeading(tag);
584
+ this.showHeadingMenu = false;
585
+ this.emitValue();
586
+ this.takeSnapshot('heading');
587
+ }
588
+ onPickSpacing(kind, target, value) {
589
+ this.focusEditor();
590
+ this.restoreSelection();
591
+ // Bevorzugt: direkt auf die Selektion anwenden (inline Wrapper)
592
+ if (!this.applySpacingToSelection(kind, target, value)) {
593
+ // Fallback: Block-Spacing, wenn Auswahl blockübergreifend ist
594
+ this.setBlockSpacing(kind, target, value);
595
+ }
596
+ this.showSpacingMenu = false;
597
+ this.emitValue();
598
+ this.takeSnapshot('spacing');
599
+ }
600
+ // ControlValueAccessor
601
+ writeValue(value) {
602
+ const el = this.editorRef?.nativeElement;
603
+ if (!el)
604
+ return;
605
+ el.innerHTML = value || '';
606
+ // Initial snapshot nur einmal anlegen
607
+ if (this.history.length === 0) {
608
+ this.takeSnapshot('init');
609
+ }
610
+ }
611
+ registerOnChange(fn) { this.onChange = fn; }
612
+ registerOnTouched(fn) { this.onTouched = fn; }
613
+ setDisabledState(isDisabled) { this.disabled = isDisabled; }
614
+ // Toolbar actions using document.execCommand (deprecated but broadly supported)
615
+ cmd(command, value) {
616
+ // Restore last selection from editor before applying an action triggered by toolbar
617
+ this.focusEditor();
618
+ this.restoreSelection();
619
+ // Bevorzugt: eigene Range-basierte Implementierungen
620
+ switch (command) {
621
+ case 'bold':
622
+ this.applyInlineStyleSmart('fontWeight', 'bold');
623
+ return;
624
+ case 'italic':
625
+ this.applyInlineStyleSmart('fontStyle', 'italic');
626
+ return;
627
+ case 'underline':
628
+ this.applyInlineStyleSmart('textDecoration', 'underline');
629
+ return;
630
+ case 'foreColor':
631
+ this.applyInlineStyle('color', value || '');
632
+ return;
633
+ case 'hiliteColor':
634
+ case 'backColor':
635
+ this.applyInlineStyle('backgroundColor', value || '');
636
+ return;
637
+ case 'createLink':
638
+ if (value) {
639
+ this.wrapSelectionWith('a', { href: value });
640
+ this.emitValue();
641
+ this.takeSnapshot('link');
642
+ }
643
+ return;
644
+ case 'insertText': {
645
+ const text = value ?? '';
646
+ this.insertTextAtSelection(text);
647
+ this.emitValue();
648
+ this.takeSnapshot('insertText');
649
+ return;
650
+ }
651
+ case 'insertUnorderedList':
652
+ this.toggleList('ul');
653
+ this.emitValue();
654
+ this.takeSnapshot('list');
655
+ return;
656
+ case 'insertOrderedList':
657
+ this.toggleList('ol');
658
+ this.emitValue();
659
+ this.takeSnapshot('list');
660
+ return;
661
+ case 'justifyLeft':
662
+ this.alignBlocks('left');
663
+ this.emitValue();
664
+ this.takeSnapshot('align');
665
+ return;
666
+ case 'justifyCenter':
667
+ this.alignBlocks('center');
668
+ this.emitValue();
669
+ this.takeSnapshot('align');
670
+ return;
671
+ case 'justifyRight':
672
+ this.alignBlocks('right');
673
+ this.emitValue();
674
+ this.takeSnapshot('align');
675
+ return;
676
+ case 'justifyFull':
677
+ this.alignBlocks('justify');
678
+ this.emitValue();
679
+ this.takeSnapshot('align');
680
+ return;
681
+ }
682
+ // Fallback (deprecated): nur für Format entfernen / Links lösen als Übergang
683
+ try {
684
+ // Hinweis im Dev-Mode ausgeben
685
+ if (!('___rteWarned' in this)) {
686
+ console.warn('[richtext-editor] document.execCommand ist deprecated; Fallback wird nur für removeFormat/unlink verwendet.');
687
+ this.___rteWarned = true;
688
+ }
689
+ if (command === 'removeFormat' || command === 'unlink') {
690
+ document.execCommand(command, false, value);
691
+ }
692
+ }
693
+ catch {
694
+ // Ignorieren, wenn nicht unterstützt
695
+ }
696
+ this.emitValue();
697
+ this.takeSnapshot('fallback');
698
+ }
699
+ applyFont(font) {
700
+ if (!font)
701
+ return;
702
+ this.applyInlineStyle('fontFamily', font);
703
+ }
704
+ applyFontSize(sizePx) {
705
+ if (!sizePx)
706
+ return;
707
+ const size = Number(sizePx);
708
+ if (!Number.isFinite(size))
709
+ return;
710
+ this.applyInlineStyle('fontSize', `${size}px`);
711
+ }
712
+ applyColor(color) {
713
+ this.focusEditor();
714
+ this.restoreSelection();
715
+ const sel = window.getSelection();
716
+ if (!sel || sel.rangeCount === 0)
717
+ return;
718
+ const r = sel.getRangeAt(0);
719
+ if (r.collapsed) {
720
+ // Firefox/Edge: wenn keine explizite Auswahl, erweitere auf nächstes Wort
721
+ this.expandRangeToWord(r);
722
+ }
723
+ this.applyInlineStyle('color', color);
724
+ }
725
+ applyHighlight(color) {
726
+ this.focusEditor();
727
+ this.restoreSelection();
728
+ const sel = window.getSelection();
729
+ if (!sel || sel.rangeCount === 0)
730
+ return;
731
+ const r = sel.getRangeAt(0);
732
+ if (r.collapsed) {
733
+ this.expandRangeToWord(r);
734
+ }
735
+ // Some browsers use 'hiliteColor', others 'backColor'
736
+ this.applyInlineStyle('backgroundColor', color);
737
+ }
738
+ expandRangeToWord(range) {
739
+ try {
740
+ const editor = this.editorRef.nativeElement;
741
+ let node = range.startContainer;
742
+ if (node.nodeType !== Node.TEXT_NODE) {
743
+ // Versuche, einen Textknoten in der Nähe zu finden
744
+ const walker = document.createTreeWalker(editor, NodeFilter.SHOW_TEXT);
745
+ let found = null;
746
+ while (walker.nextNode()) {
747
+ const n = walker.currentNode;
748
+ if (n.parentElement && editor.contains(n.parentElement)) {
749
+ found = n;
750
+ break;
751
+ }
752
+ }
753
+ if (found)
754
+ node = found;
755
+ }
756
+ const text = node.nodeType === Node.TEXT_NODE ? node.data : '';
757
+ let start = 0, end = text.length;
758
+ // einfache Wortgrenzen-Heuristik
759
+ const pos = range.startOffset;
760
+ for (let i = pos; i > 0; i--) {
761
+ if (/\s/.test(text[i - 1])) {
762
+ start = i;
763
+ break;
764
+ }
765
+ }
766
+ for (let i = pos; i < text.length; i++) {
767
+ if (/\s/.test(text[i])) {
768
+ end = i;
769
+ break;
770
+ }
771
+ }
772
+ range.setStart(node, start);
773
+ range.setEnd(node, end);
774
+ const sel = window.getSelection();
775
+ if (sel) {
776
+ sel.removeAllRanges();
777
+ sel.addRange(range);
778
+ }
779
+ }
780
+ catch { /* noop */ }
781
+ }
782
+ insertLink() {
783
+ const url = prompt('Link-URL eingeben:', 'https://');
784
+ if (url) {
785
+ this.cmd('createLink', url);
786
+ }
787
+ }
788
+ removeFormat() {
789
+ this.cmd('removeFormat');
790
+ this.cmd('unlink');
791
+ }
792
+ onInput() {
793
+ this.emitValue();
794
+ this.scheduleSnapshot();
795
+ }
796
+ onKeyup(_evt) {
797
+ this.saveSelection();
798
+ this.emitValue();
799
+ this.updateInlineStates();
800
+ }
801
+ onPaste(evt) {
802
+ // Optional: Clean paste to plain text while keeping basic formatting minimal.
803
+ if (!evt.clipboardData)
804
+ return;
805
+ evt.preventDefault();
806
+ const text = evt.clipboardData.getData('text/plain');
807
+ this.insertTextAtSelection(text);
808
+ this.emitValue();
809
+ this.takeSnapshot('paste');
810
+ }
811
+ emitValue() {
812
+ // Clean up empty style attributes and redundant spans before emitting
813
+ this.cleanupEmptyStylesAndSpans();
814
+ const val = this.editorRef.nativeElement.innerHTML;
815
+ this.onChange(val);
816
+ this.valueChange.emit(val);
817
+ this.updateInlineStates();
818
+ }
819
+ // Remove empty style attributes (style="") and unwrap spans without any attributes
820
+ cleanupEmptyStylesAndSpans(root) {
821
+ const editor = root || this.editorRef.nativeElement;
822
+ const toUnwrap = [];
823
+ const walker = document.createTreeWalker(editor, NodeFilter.SHOW_ELEMENT);
824
+ while (walker.nextNode()) {
825
+ const el = walker.currentNode;
826
+ if (el.hasAttribute('style')) {
827
+ // If no style declarations remain, drop the attribute
828
+ const cssText = el.getAttribute('style') || '';
829
+ const byApiEmpty = (el.style ? el.style.length === 0 : false);
830
+ const normalized = cssText.replace(/[\s;]/g, '');
831
+ if (byApiEmpty || normalized.length === 0) {
832
+ el.removeAttribute('style');
833
+ }
834
+ }
835
+ // Unwrap spans that have no attributes left
836
+ if (el.tagName === 'SPAN' && el.attributes.length === 0) {
837
+ toUnwrap.push(el);
838
+ }
839
+ }
840
+ for (const span of toUnwrap) {
841
+ while (span.firstChild)
842
+ span.parentNode?.insertBefore(span.firstChild, span);
843
+ span.remove();
844
+ }
845
+ }
846
+ // History API
847
+ get canUndo() { return this.historyIndex > 0; }
848
+ get canRedo() { return this.historyIndex >= 0 && this.historyIndex < this.history.length - 1; }
849
+ undo() {
850
+ if (!this.canUndo)
851
+ return;
852
+ this.applySnapshot(this.historyIndex - 1);
853
+ }
854
+ redo() {
855
+ if (!this.canRedo)
856
+ return;
857
+ this.applySnapshot(this.historyIndex + 1);
858
+ }
859
+ scheduleSnapshot() {
860
+ if (this.isRestoringHistory)
861
+ return;
862
+ if (this.snapshotTimer)
863
+ clearTimeout(this.snapshotTimer);
864
+ this.snapshotTimer = setTimeout(() => this.takeSnapshot('input'), 250);
865
+ }
866
+ takeSnapshot(_reason) {
867
+ if (this.isRestoringHistory)
868
+ return;
869
+ const editor = this.editorRef.nativeElement;
870
+ const html = editor.innerHTML;
871
+ const sel = window.getSelection();
872
+ let rangeData = null;
873
+ if (sel && sel.rangeCount > 0) {
874
+ const r = sel.getRangeAt(0);
875
+ if (this.isRangeInEditor(r)) {
876
+ const ser = this.serializeRange(r);
877
+ if (ser)
878
+ rangeData = ser;
879
+ }
880
+ }
881
+ // Verhindere Duplikate nacheinander
882
+ const last = this.history[this.historyIndex];
883
+ if (last && last.html === html)
884
+ return;
885
+ // Zukunft verwerfen bei neuem Snapshot
886
+ if (this.historyIndex < this.history.length - 1) {
887
+ this.history = this.history.slice(0, this.historyIndex + 1);
888
+ }
889
+ this.history.push({ html, range: rangeData });
890
+ this.historyIndex = this.history.length - 1;
891
+ // Begrenze Historie
892
+ const MAX = 50;
893
+ if (this.history.length > MAX) {
894
+ const drop = this.history.length - MAX;
895
+ this.history.splice(0, drop);
896
+ this.historyIndex -= drop;
897
+ if (this.historyIndex < 0)
898
+ this.historyIndex = 0;
899
+ }
900
+ }
901
+ applySnapshot(index) {
902
+ if (index < 0 || index >= this.history.length)
903
+ return;
904
+ const snap = this.history[index];
905
+ const editor = this.editorRef.nativeElement;
906
+ this.isRestoringHistory = true;
907
+ editor.innerHTML = snap.html;
908
+ // Selektion wiederherstellen
909
+ if (snap.range) {
910
+ this.restoreSerializedRange(snap.range);
911
+ }
912
+ else {
913
+ // Cursor ans Ende
914
+ const sel = window.getSelection();
915
+ if (sel) {
916
+ sel.removeAllRanges();
917
+ const r = document.createRange();
918
+ r.selectNodeContents(editor);
919
+ r.collapse(false);
920
+ sel.addRange(r);
921
+ }
922
+ }
923
+ this.historyIndex = index;
924
+ this.isRestoringHistory = false;
925
+ // Werte emittieren nach Restore
926
+ this.emitValue();
927
+ }
928
+ serializeRange(range) {
929
+ const s = this.nodePathFromNode(range.startContainer);
930
+ const e = this.nodePathFromNode(range.endContainer);
931
+ if (!s || !e)
932
+ return null;
933
+ return {
934
+ startPath: s,
935
+ startOffset: range.startOffset,
936
+ endPath: e,
937
+ endOffset: range.endOffset,
938
+ };
939
+ }
940
+ restoreSerializedRange(data) {
941
+ const editor = this.editorRef.nativeElement;
942
+ const startNode = this.nodeFromPath(data.startPath);
943
+ const endNode = this.nodeFromPath(data.endPath);
944
+ if (!startNode || !endNode)
945
+ return;
946
+ const r = document.createRange();
947
+ try {
948
+ r.setStart(startNode, Math.min(data.startOffset, this.maxOffset(startNode)));
949
+ r.setEnd(endNode, Math.min(data.endOffset, this.maxOffset(endNode)));
950
+ }
951
+ catch {
952
+ r.selectNodeContents(editor);
953
+ r.collapse(false);
954
+ }
955
+ const sel = window.getSelection();
956
+ if (sel) {
957
+ sel.removeAllRanges();
958
+ sel.addRange(r);
959
+ }
960
+ }
961
+ nodePathFromNode(node) {
962
+ const editor = this.editorRef.nativeElement;
963
+ const path = [];
964
+ let n = node;
965
+ while (n && n !== editor) {
966
+ const pnode = n.parentNode;
967
+ if (!pnode)
968
+ return null;
969
+ const idx = Array.prototype.indexOf.call(pnode.childNodes, n);
970
+ path.push(idx);
971
+ n = pnode;
972
+ }
973
+ if (n !== editor)
974
+ return null;
975
+ path.reverse();
976
+ return path;
977
+ }
978
+ nodeFromPath(path) {
979
+ const editor = this.editorRef.nativeElement;
980
+ let n = editor;
981
+ for (const idx of path) {
982
+ if (!n.childNodes || idx < 0 || idx >= n.childNodes.length)
983
+ return null;
984
+ n = n.childNodes[idx];
985
+ }
986
+ return n;
987
+ }
988
+ maxOffset(node) {
989
+ if (node.nodeType === Node.TEXT_NODE)
990
+ return node.data.length;
991
+ return node.childNodes.length;
992
+ }
993
+ focusEditor() {
994
+ const el = this.editorRef.nativeElement;
995
+ if (document.activeElement !== el) {
996
+ el.focus();
997
+ }
998
+ }
999
+ // Minimal inline style applier for inline styles
1000
+ applyInlineStyle(cssProp, value) {
1001
+ this.focusEditor();
1002
+ this.restoreSelection();
1003
+ const sel = window.getSelection();
1004
+ if (!sel || sel.rangeCount === 0)
1005
+ return;
1006
+ const range = sel.getRangeAt(0);
1007
+ if (range.collapsed)
1008
+ return;
1009
+ const span = document.createElement('span');
1010
+ span.style[cssProp] = value;
1011
+ // Robuster als surroundContents: extract + wrap + insert
1012
+ const frag = range.extractContents();
1013
+ span.appendChild(frag);
1014
+ range.insertNode(span);
1015
+ // Cursor hinter das eingefügte Element setzen
1016
+ sel.removeAllRanges();
1017
+ const after = document.createRange();
1018
+ after.setStartAfter(span);
1019
+ after.collapse(true);
1020
+ sel.addRange(after);
1021
+ this.emitValue();
1022
+ this.takeSnapshot('style');
1023
+ }
1024
+ applyInlineStyleSmart(cssProp, value) {
1025
+ this.focusEditor();
1026
+ this.restoreSelection();
1027
+ const sel = window.getSelection();
1028
+ if (!sel || sel.rangeCount === 0)
1029
+ return;
1030
+ const range = sel.getRangeAt(0);
1031
+ if (range.collapsed)
1032
+ return;
1033
+ const isBlockTag = (tag) => /^(P|DIV|PRE|H1|H2|H3|H4|H5|H6|LI|TD|TH)$/i.test(tag);
1034
+ const getBlockAncestor = (node) => {
1035
+ const editor = this.editorRef.nativeElement;
1036
+ let el = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
1037
+ while (el && el !== editor && !isBlockTag(el.tagName))
1038
+ el = el.parentElement;
1039
+ return (el && el !== editor) ? el : null;
1040
+ };
1041
+ const startBlock = getBlockAncestor(range.startContainer);
1042
+ const endBlock = getBlockAncestor(range.endContainer);
1043
+ const crossesBlocks = !!startBlock && !!endBlock && startBlock !== endBlock;
1044
+ if (!crossesBlocks) {
1045
+ // Single block selection: keep using inline wrapper for precise range
1046
+ this.applyInlineStyle(cssProp, value);
1047
+ return;
1048
+ }
1049
+ // Multi-block selection: apply style to each intersecting block element (avoid invalid span structure)
1050
+ const common = range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE
1051
+ ? range.commonAncestorContainer
1052
+ : (range.commonAncestorContainer.parentElement || this.editorRef.nativeElement);
1053
+ const walker = document.createTreeWalker(common, NodeFilter.SHOW_ELEMENT);
1054
+ const styled = new Set();
1055
+ while (walker.nextNode()) {
1056
+ const el = walker.currentNode;
1057
+ if (!isBlockTag(el.tagName))
1058
+ continue;
1059
+ try {
1060
+ if (range.intersectsNode && range.intersectsNode(el))
1061
+ styled.add(el);
1062
+ }
1063
+ catch { }
1064
+ }
1065
+ for (const el of styled) {
1066
+ el.style[cssProp] = value;
1067
+ }
1068
+ this.emitValue();
1069
+ this.takeSnapshot('style-blocks');
1070
+ }
1071
+ getIntersectingBlocks(range) {
1072
+ const isBlockTag = (tag) => /^(P|DIV|PRE|H1|H2|H3|H4|H5|H6|LI|TD|TH)$/i.test(tag);
1073
+ const common = range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE
1074
+ ? range.commonAncestorContainer
1075
+ : (range.commonAncestorContainer.parentElement || this.editorRef.nativeElement);
1076
+ const walker = document.createTreeWalker(common, NodeFilter.SHOW_ELEMENT);
1077
+ const blocks = [];
1078
+ while (walker.nextNode()) {
1079
+ const el = walker.currentNode;
1080
+ if (!isBlockTag(el.tagName))
1081
+ continue;
1082
+ try {
1083
+ if (range.intersectsNode && range.intersectsNode(el))
1084
+ blocks.push(el);
1085
+ }
1086
+ catch { }
1087
+ }
1088
+ return blocks;
1089
+ }
1090
+ // Variant of applyInlineStyle that returns the created span and does not emit/snapshot by itself
1091
+ wrapSelectionInline(cssProp, value) {
1092
+ this.focusEditor();
1093
+ this.restoreSelection();
1094
+ const sel = window.getSelection();
1095
+ if (!sel || sel.rangeCount === 0)
1096
+ return null;
1097
+ const range = sel.getRangeAt(0);
1098
+ if (range.collapsed)
1099
+ return null;
1100
+ const span = document.createElement('span');
1101
+ span.style[cssProp] = value;
1102
+ const frag = range.extractContents();
1103
+ span.appendChild(frag);
1104
+ range.insertNode(span);
1105
+ // Restore selection just after for continuity
1106
+ sel.removeAllRanges();
1107
+ const after = document.createRange();
1108
+ after.setStartAfter(span);
1109
+ after.collapse(true);
1110
+ sel.addRange(after);
1111
+ return span;
1112
+ }
1113
+ isBoldCarrier(el) {
1114
+ if (!el)
1115
+ return false;
1116
+ const tag = el.tagName;
1117
+ if (tag === 'B' || tag === 'STRONG')
1118
+ return true;
1119
+ const fw = el.style?.fontWeight || '';
1120
+ if (fw && fw !== 'normal' && fw !== '400')
1121
+ return true;
1122
+ return false;
1123
+ }
1124
+ boldAncestorContainingRange(range) {
1125
+ let el = range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE
1126
+ ? range.commonAncestorContainer
1127
+ : range.commonAncestorContainer.parentElement;
1128
+ const editor = this.editorRef.nativeElement;
1129
+ while (el && el !== editor) {
1130
+ if (this.isBoldCarrier(el)) {
1131
+ // ensure el contains both boundary containers fully
1132
+ const sc = range.startContainer;
1133
+ const ec = range.endContainer;
1134
+ if (el.contains(sc) && el.contains(ec))
1135
+ return el;
1136
+ }
1137
+ el = el.parentElement;
1138
+ }
1139
+ return null;
1140
+ }
1141
+ ancestorContainingRange(range, isCarrier) {
1142
+ let el = range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE
1143
+ ? range.commonAncestorContainer
1144
+ : range.commonAncestorContainer.parentElement;
1145
+ const editor = this.editorRef.nativeElement;
1146
+ while (el && el !== editor) {
1147
+ if (isCarrier(el)) {
1148
+ const sc = range.startContainer;
1149
+ const ec = range.endContainer;
1150
+ if (el.contains(sc) && el.contains(ec))
1151
+ return el;
1152
+ }
1153
+ el = el.parentElement;
1154
+ }
1155
+ return null;
1156
+ }
1157
+ splitCarrierAroundSelection(range, carrier) {
1158
+ const parent = carrier.parentNode;
1159
+ if (!parent)
1160
+ return;
1161
+ const rRight = document.createRange();
1162
+ rRight.setStart(range.endContainer, range.endOffset);
1163
+ rightGuard(carrier, rRight);
1164
+ const rightFrag = rRight.extractContents();
1165
+ const rLeft = document.createRange();
1166
+ rLeft.setStart(carrier, 0);
1167
+ leftGuard(range, rLeft);
1168
+ const leftFrag = rLeft.extractContents();
1169
+ if (leftFrag.childNodes.length) {
1170
+ const leftClone = carrier.cloneNode(false);
1171
+ leftClone.appendChild(leftFrag);
1172
+ parent.insertBefore(leftClone, carrier);
1173
+ }
1174
+ let insertAfterRef = carrier;
1175
+ if (rightFrag.childNodes.length) {
1176
+ const rightClone = carrier.cloneNode(false);
1177
+ rightClone.appendChild(rightFrag);
1178
+ parent.insertBefore(rightClone, carrier.nextSibling);
1179
+ insertAfterRef = rightClone;
1180
+ }
1181
+ while (carrier.firstChild)
1182
+ parent.insertBefore(carrier.firstChild, insertAfterRef);
1183
+ carrier.remove();
1184
+ function rightGuard(node, r) {
1185
+ try {
1186
+ r.setEnd(node, node.childNodes.length);
1187
+ }
1188
+ catch { }
1189
+ }
1190
+ function leftGuard(rng, r) {
1191
+ try {
1192
+ r.setEnd(rng.startContainer, rng.startOffset);
1193
+ }
1194
+ catch { }
1195
+ }
1196
+ }
1197
+ // Unwrap bold formatting only for the selected content inside a containing bold ancestor by splitting it into left/selection/right parts
1198
+ deselectBoldBySplitting(range) {
1199
+ const ancestor = this.boldAncestorContainingRange(range);
1200
+ if (!ancestor)
1201
+ return false;
1202
+ this.splitCarrierAroundSelection(range, ancestor);
1203
+ return true;
1204
+ }
1205
+ deselectItalicBySplitting(range) {
1206
+ const anc = this.ancestorContainingRange(range, el => this.isItalicCarrier(el));
1207
+ if (!anc)
1208
+ return false;
1209
+ this.splitCarrierAroundSelection(range, anc);
1210
+ return true;
1211
+ }
1212
+ deselectUnderlineBySplitting(range) {
1213
+ const anc = this.ancestorContainingRange(range, el => this.isUnderlineCarrier(el));
1214
+ if (!anc)
1215
+ return false;
1216
+ this.splitCarrierAroundSelection(range, anc);
1217
+ return true;
1218
+ }
1219
+ cleanupEmptyItalicSpans(root) {
1220
+ const toRemove = [];
1221
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
1222
+ while (walker.nextNode()) {
1223
+ const el = walker.currentNode;
1224
+ const isItalic = (el.tagName === 'I' || el.tagName === 'EM' || (el.tagName === 'SPAN' && el.style.fontStyle && el.style.fontStyle !== 'normal'));
1225
+ if (!isItalic)
1226
+ continue;
1227
+ const text = el.textContent ?? '';
1228
+ const normalized = text.replace(/[\u00A0\u200B\s]/g, '');
1229
+ const onlyWhitespace = !el.firstChild || (normalized.length === 0 && el.querySelectorAll('*').length === 0);
1230
+ if (onlyWhitespace)
1231
+ toRemove.push(el);
1232
+ }
1233
+ for (const el of toRemove)
1234
+ el.remove();
1235
+ }
1236
+ cleanupEmptyUnderlineSpans(root) {
1237
+ const toRemove = [];
1238
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
1239
+ while (walker.nextNode()) {
1240
+ const el = walker.currentNode;
1241
+ const isU = (el.tagName === 'U' || (el.tagName === 'SPAN' && typeof el.style.textDecoration === 'string' && el.style.textDecoration.includes('underline')));
1242
+ if (!isU)
1243
+ continue;
1244
+ const text = el.textContent ?? '';
1245
+ const normalized = text.replace(/[\u00A0\u200B\s]/g, '');
1246
+ const onlyWhitespace = !el.firstChild || (normalized.length === 0 && el.querySelectorAll('*').length === 0);
1247
+ if (onlyWhitespace)
1248
+ toRemove.push(el);
1249
+ }
1250
+ for (const el of toRemove)
1251
+ el.remove();
1252
+ }
1253
+ // Remove bold wrappers and inline font-weight style inside a container
1254
+ stripBoldWithin(container) {
1255
+ const toProcess = [];
1256
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT);
1257
+ while (walker.nextNode()) {
1258
+ toProcess.push(walker.currentNode);
1259
+ }
1260
+ for (const el of toProcess) {
1261
+ if (el.tagName === 'B' || el.tagName === 'STRONG') {
1262
+ while (el.firstChild)
1263
+ el.parentNode?.insertBefore(el.firstChild, el);
1264
+ el.remove();
1265
+ continue;
1266
+ }
1267
+ if (el.tagName === 'SPAN') {
1268
+ if (el.style.fontWeight)
1269
+ el.style.removeProperty('font-weight');
1270
+ // Clean empty style spans
1271
+ if (!el.getAttribute('style')) {
1272
+ while (el.firstChild)
1273
+ el.parentNode?.insertBefore(el.firstChild, el);
1274
+ el.remove();
1275
+ }
1276
+ }
1277
+ }
1278
+ }
1279
+ // Pull the given node out of any bold carrier ancestors by splitting them around the node
1280
+ liftOutOfBoldAncestors(node) {
1281
+ const editor = this.editorRef.nativeElement;
1282
+ let currentBold = this.findClosest(node, 'b,strong,span[style*="font-weight"]');
1283
+ while (currentBold && currentBold !== editor) {
1284
+ this.splitOutOfAncestor(currentBold, node);
1285
+ currentBold = this.findClosest(node, 'b,strong,span[style*="font-weight"]');
1286
+ }
1287
+ }
1288
+ // Split arbitrary ancestor element so that `node` becomes a sibling outside of it, preserving order even if `node` is nested deeply
1289
+ splitOutOfAncestor(ancestor, node) {
1290
+ // First, climb from node up to direct child of ancestor, splitting wrappers after `node` at each level
1291
+ let child = node;
1292
+ let parent = child.parentElement;
1293
+ while (parent && parent !== ancestor) {
1294
+ const right = parent.cloneNode(false);
1295
+ // move siblings after `child` into right clone
1296
+ while (child.nextSibling)
1297
+ right.appendChild(child.nextSibling);
1298
+ // insert right after parent
1299
+ parent.after(right);
1300
+ // ascend
1301
+ child = parent;
1302
+ parent = child.parentElement;
1303
+ }
1304
+ if (!parent)
1305
+ return;
1306
+ // Now `child` is a direct child of ancestor
1307
+ const before = ancestor.cloneNode(false);
1308
+ while (ancestor.firstChild && ancestor.firstChild !== child) {
1309
+ before.appendChild(ancestor.firstChild);
1310
+ }
1311
+ const after = ancestor.cloneNode(false);
1312
+ while (child.nextSibling)
1313
+ after.appendChild(child.nextSibling);
1314
+ const gp = ancestor.parentNode;
1315
+ if (!gp)
1316
+ return;
1317
+ if (before.childNodes.length)
1318
+ gp.insertBefore(before, ancestor);
1319
+ // move node out, place where ancestor was
1320
+ gp.insertBefore(node, ancestor);
1321
+ if (after.childNodes.length)
1322
+ gp.insertBefore(after, ancestor);
1323
+ ancestor.remove();
1324
+ }
1325
+ cleanupEmptyBoldSpans(root) {
1326
+ const toRemove = [];
1327
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
1328
+ while (walker.nextNode()) {
1329
+ const el = walker.currentNode;
1330
+ const isBoldEl = (el.tagName === 'B' || el.tagName === 'STRONG' || (el.tagName === 'SPAN' && el.style.fontWeight && el.style.fontWeight !== 'normal' && el.style.fontWeight !== '400'));
1331
+ if (!isBoldEl)
1332
+ continue;
1333
+ // remove if no children or only whitespace (including NBSP \u00A0 and zero-width space \u200B)
1334
+ const text = el.textContent ?? '';
1335
+ const normalized = text.replace(/[\u00A0\u200B\s]/g, '');
1336
+ const onlyWhitespace = !el.firstChild || (normalized.length === 0 && el.querySelectorAll('*').length === 0);
1337
+ if (onlyWhitespace) {
1338
+ toRemove.push(el);
1339
+ }
1340
+ }
1341
+ for (const el of toRemove)
1342
+ el.remove();
1343
+ }
1344
+ maybeUnwrapNormalSpan(span) {
1345
+ if (span.tagName !== 'SPAN')
1346
+ return;
1347
+ // If no bold applies from ancestors, drop the 400 style and unwrap if style empty
1348
+ const hasBoldAbove = this.isNodeBold(span.parentElement);
1349
+ if (!hasBoldAbove) {
1350
+ if (span.style.fontWeight === '400')
1351
+ span.style.removeProperty('font-weight');
1352
+ if (!span.getAttribute('style')) {
1353
+ while (span.firstChild)
1354
+ span.parentNode?.insertBefore(span.firstChild, span);
1355
+ span.remove();
1356
+ }
1357
+ }
1358
+ }
1359
+ wrapSelectionWith(tag, attrs) {
1360
+ const sel = window.getSelection();
1361
+ if (!sel || sel.rangeCount === 0)
1362
+ return;
1363
+ const range = sel.getRangeAt(0);
1364
+ if (range.collapsed)
1365
+ return;
1366
+ const el = document.createElement(tag);
1367
+ if (attrs) {
1368
+ for (const [k, v] of Object.entries(attrs)) {
1369
+ if (v != null)
1370
+ el.setAttribute(k, v);
1371
+ }
1372
+ }
1373
+ const frag = range.extractContents();
1374
+ el.appendChild(frag);
1375
+ range.insertNode(el);
1376
+ // Auswahl hinter das Element setzen
1377
+ sel.removeAllRanges();
1378
+ const after = document.createRange();
1379
+ after.setStartAfter(el);
1380
+ after.collapse(true);
1381
+ sel.addRange(after);
1382
+ }
1383
+ insertTextAtSelection(text) {
1384
+ const sel = window.getSelection();
1385
+ if (!sel || sel.rangeCount === 0)
1386
+ return;
1387
+ const range = sel.getRangeAt(0);
1388
+ range.deleteContents();
1389
+ const node = document.createTextNode(text);
1390
+ range.insertNode(node);
1391
+ // Cursor ans Ende des eingefügten Textes
1392
+ sel.removeAllRanges();
1393
+ const after = document.createRange();
1394
+ after.setStartAfter(node);
1395
+ after.collapse(true);
1396
+ sel.addRange(after);
1397
+ }
1398
+ // ----- Inline toggle logic (Bold/Italic/Underline) -----
1399
+ updateInlineStates() {
1400
+ const sel = window.getSelection();
1401
+ if (!sel || sel.rangeCount === 0) {
1402
+ this.isBoldActive = this.isItalicActive = this.isUnderlineActive = false;
1403
+ return;
1404
+ }
1405
+ const range = sel.getRangeAt(0);
1406
+ if (!this.isRangeInEditor(range)) {
1407
+ this.isBoldActive = this.isItalicActive = this.isUnderlineActive = false;
1408
+ return;
1409
+ }
1410
+ if (range.collapsed) {
1411
+ const node = range.startContainer.nodeType === Node.ELEMENT_NODE ? range.startContainer : range.startContainer.parentElement;
1412
+ this.isBoldActive = this.isNodeBold(node);
1413
+ this.isItalicActive = this.isNodeItalic(node);
1414
+ this.isUnderlineActive = this.isNodeUnderline(node);
1415
+ return;
1416
+ }
1417
+ this.isBoldActive = this.computeBoldAnyForRange(range);
1418
+ this.isItalicActive = this.computeItalicAnyForRange(range);
1419
+ this.isUnderlineActive = this.computeUnderlineAnyForRange(range);
1420
+ // ensure UI reflects changes immediately
1421
+ try {
1422
+ this.cdr.detectChanges();
1423
+ }
1424
+ catch { }
1425
+ }
1426
+ computeBoldAnyForRange(range) {
1427
+ const root = this.editorRef.nativeElement;
1428
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
1429
+ while (walker.nextNode()) {
1430
+ const n = walker.currentNode;
1431
+ if (!n.data.trim())
1432
+ continue;
1433
+ try {
1434
+ if (range.intersectsNode(n)) {
1435
+ if (this.isNodeBold(n.parentElement))
1436
+ return true;
1437
+ }
1438
+ }
1439
+ catch { }
1440
+ }
1441
+ return false;
1442
+ }
1443
+ computeItalicAnyForRange(range) {
1444
+ const root = this.editorRef.nativeElement;
1445
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
1446
+ while (walker.nextNode()) {
1447
+ const n = walker.currentNode;
1448
+ if (!n.data.trim())
1449
+ continue;
1450
+ try {
1451
+ if (range.intersectsNode(n)) {
1452
+ if (this.isNodeItalic(n.parentElement))
1453
+ return true;
1454
+ }
1455
+ }
1456
+ catch { }
1457
+ }
1458
+ return false;
1459
+ }
1460
+ computeUnderlineAnyForRange(range) {
1461
+ const root = this.editorRef.nativeElement;
1462
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
1463
+ while (walker.nextNode()) {
1464
+ const n = walker.currentNode;
1465
+ if (!n.data.trim())
1466
+ continue;
1467
+ try {
1468
+ if (range.intersectsNode(n)) {
1469
+ if (this.isNodeUnderline(n.parentElement))
1470
+ return true;
1471
+ }
1472
+ }
1473
+ catch { }
1474
+ }
1475
+ return false;
1476
+ }
1477
+ isNodeBold(node) {
1478
+ let el = node;
1479
+ while (el) {
1480
+ if (el.tagName === 'B' || el.tagName === 'STRONG')
1481
+ return true;
1482
+ const cs = getComputedStyle(el);
1483
+ if (cs.fontWeight && cs.fontWeight !== 'normal' && cs.fontWeight !== '400') {
1484
+ const w = cs.fontWeight === 'bold' ? 700 : parseInt(cs.fontWeight, 10);
1485
+ if (!Number.isNaN(w) && w >= 600)
1486
+ return true;
1487
+ }
1488
+ el = el.parentElement;
1489
+ }
1490
+ return false;
1491
+ }
1492
+ isNodeItalic(node) {
1493
+ let el = node;
1494
+ while (el) {
1495
+ if (el.tagName === 'I' || el.tagName === 'EM')
1496
+ return true;
1497
+ const cs = getComputedStyle(el);
1498
+ if (cs.fontStyle && cs.fontStyle !== 'normal')
1499
+ return true;
1500
+ el = el.parentElement;
1501
+ }
1502
+ return false;
1503
+ }
1504
+ isNodeUnderline(node) {
1505
+ let el = node;
1506
+ while (el) {
1507
+ if (el.tagName === 'U')
1508
+ return true;
1509
+ const cs = getComputedStyle(el);
1510
+ if (cs.textDecorationLine && cs.textDecorationLine.includes('underline'))
1511
+ return true;
1512
+ el = el.parentElement;
1513
+ }
1514
+ return false;
1515
+ }
1516
+ isItalicCarrier(el) {
1517
+ if (!el)
1518
+ return false;
1519
+ const tag = el.tagName;
1520
+ if (tag === 'I' || tag === 'EM')
1521
+ return true;
1522
+ const fs = el.style?.fontStyle || '';
1523
+ return !!fs && fs !== 'normal';
1524
+ }
1525
+ isUnderlineCarrier(el) {
1526
+ if (!el)
1527
+ return false;
1528
+ const tag = el.tagName;
1529
+ if (tag === 'U')
1530
+ return true;
1531
+ const td = el.style?.textDecoration || '';
1532
+ return typeof td === 'string' && td.includes('underline');
1533
+ }
1534
+ computeBoldActiveForRange(range) {
1535
+ const root = this.isRangeInEditor(range) ? this.editorRef.nativeElement : range.commonAncestorContainer;
1536
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
1537
+ const nodes = [];
1538
+ while (walker.nextNode()) {
1539
+ const n = walker.currentNode;
1540
+ if (!n.data.trim())
1541
+ continue;
1542
+ try {
1543
+ if (range.intersectsNode(n))
1544
+ nodes.push(n);
1545
+ }
1546
+ catch { /* Safari may throw if node not in same tree */ }
1547
+ }
1548
+ if (nodes.length === 0)
1549
+ return false;
1550
+ return nodes.every(t => this.isNodeBold(t.parentElement));
1551
+ }
1552
+ computeItalicActiveForRange(range) {
1553
+ const root = this.isRangeInEditor(range) ? this.editorRef.nativeElement : range.commonAncestorContainer;
1554
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
1555
+ const nodes = [];
1556
+ while (walker.nextNode()) {
1557
+ const n = walker.currentNode;
1558
+ if (!n.data.trim())
1559
+ continue;
1560
+ try {
1561
+ if (range.intersectsNode(n))
1562
+ nodes.push(n);
1563
+ }
1564
+ catch { }
1565
+ }
1566
+ if (nodes.length === 0)
1567
+ return false;
1568
+ return nodes.every(t => this.isNodeItalic(t.parentElement));
1569
+ }
1570
+ computeUnderlineActiveForRange(range) {
1571
+ const root = this.isRangeInEditor(range) ? this.editorRef.nativeElement : range.commonAncestorContainer;
1572
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
1573
+ const nodes = [];
1574
+ while (walker.nextNode()) {
1575
+ const n = walker.currentNode;
1576
+ if (!n.data.trim())
1577
+ continue;
1578
+ try {
1579
+ if (range.intersectsNode(n))
1580
+ nodes.push(n);
1581
+ }
1582
+ catch { }
1583
+ }
1584
+ if (nodes.length === 0)
1585
+ return false;
1586
+ return nodes.every(t => this.isNodeUnderline(t.parentElement));
1587
+ }
1588
+ removeInlineStyleInRange(range, cssPropKebab) {
1589
+ const editor = this.editorRef.nativeElement;
1590
+ const common = range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE ? range.commonAncestorContainer : range.commonAncestorContainer.parentElement || editor;
1591
+ const affected = [];
1592
+ const walker = document.createTreeWalker(common, NodeFilter.SHOW_ELEMENT);
1593
+ while (walker.nextNode()) {
1594
+ const el = walker.currentNode;
1595
+ let matches = false;
1596
+ if (el.tagName === 'SPAN' && el.style && el.style.getPropertyValue(cssPropKebab))
1597
+ matches = true;
1598
+ if (cssPropKebab === 'font-weight' && (el.tagName === 'B' || el.tagName === 'STRONG'))
1599
+ matches = true;
1600
+ if (cssPropKebab === 'font-style' && (el.tagName === 'I' || el.tagName === 'EM'))
1601
+ matches = true;
1602
+ if (cssPropKebab === 'text-decoration' && el.tagName === 'U')
1603
+ matches = true;
1604
+ if (!matches)
1605
+ continue;
1606
+ try {
1607
+ if (range.intersectsNode ? range.intersectsNode(el) : true)
1608
+ affected.push(el);
1609
+ }
1610
+ catch { }
1611
+ }
1612
+ for (const el of affected) {
1613
+ if (el.tagName === 'B' || el.tagName === 'STRONG' || el.tagName === 'I' || el.tagName === 'EM' || el.tagName === 'U') {
1614
+ while (el.firstChild)
1615
+ el.parentNode?.insertBefore(el.firstChild, el);
1616
+ el.remove();
1617
+ continue;
1618
+ }
1619
+ el.style.removeProperty(cssPropKebab);
1620
+ if (!el.getAttribute('style')) {
1621
+ while (el.firstChild)
1622
+ el.parentNode?.insertBefore(el.firstChild, el);
1623
+ el.remove();
1624
+ }
1625
+ }
1626
+ this.emitValue();
1627
+ }
1628
+ toggleBold() {
1629
+ this.focusEditor();
1630
+ this.restoreSelection();
1631
+ const sel = window.getSelection();
1632
+ if (!sel || sel.rangeCount === 0)
1633
+ return;
1634
+ const range = sel.getRangeAt(0);
1635
+ if (range.collapsed)
1636
+ this.expandRangeToWord(range);
1637
+ const anyActive = this.computeBoldAnyForRange(range);
1638
+ const blocks = this.getIntersectingBlocks(range);
1639
+ const multiBlock = blocks.length > 1;
1640
+ if (anyActive) {
1641
+ if (multiBlock) {
1642
+ for (const b of blocks)
1643
+ b.style.removeProperty('font-weight');
1644
+ this.emitValue();
1645
+ this.updateInlineStates();
1646
+ this.takeSnapshot('toggle-bold-blocks');
1647
+ return;
1648
+ }
1649
+ // Preferred: split bold ancestor around selection so we keep surrounding text intact
1650
+ const changed = this.deselectBoldBySplitting(range);
1651
+ if (changed) {
1652
+ this.cleanupEmptyBoldSpans(this.editorRef.nativeElement);
1653
+ this.emitValue();
1654
+ }
1655
+ else {
1656
+ // Fallback: previous neutralization approach for complex cases
1657
+ const normal = this.wrapSelectionInline('fontWeight', '400');
1658
+ if (normal) {
1659
+ this.stripBoldWithin(normal);
1660
+ this.liftOutOfBoldAncestors(normal);
1661
+ this.cleanupEmptyBoldSpans(this.editorRef.nativeElement);
1662
+ this.maybeUnwrapNormalSpan(normal);
1663
+ this.emitValue();
1664
+ }
1665
+ }
1666
+ }
1667
+ else {
1668
+ if (multiBlock) {
1669
+ for (const b of blocks)
1670
+ b.style.fontWeight = 'bold';
1671
+ this.emitValue();
1672
+ this.updateInlineStates();
1673
+ this.takeSnapshot('toggle-bold-blocks');
1674
+ return;
1675
+ }
1676
+ this.applyInlineStyleSmart('fontWeight', 'bold');
1677
+ }
1678
+ this.updateInlineStates();
1679
+ this.takeSnapshot('toggle-bold');
1680
+ }
1681
+ toggleItalic() {
1682
+ this.focusEditor();
1683
+ this.restoreSelection();
1684
+ const sel = window.getSelection();
1685
+ if (!sel || sel.rangeCount === 0)
1686
+ return;
1687
+ const range = sel.getRangeAt(0);
1688
+ if (range.collapsed)
1689
+ this.expandRangeToWord(range);
1690
+ const anyActive = this.computeItalicAnyForRange(range);
1691
+ if (anyActive) {
1692
+ const changed = this.deselectItalicBySplitting(range);
1693
+ if (changed) {
1694
+ this.cleanupEmptyItalicSpans(this.editorRef.nativeElement);
1695
+ this.emitValue();
1696
+ }
1697
+ else {
1698
+ this.removeInlineStyleInRange(range, 'font-style');
1699
+ this.applyInlineStyle('fontStyle', 'normal');
1700
+ }
1701
+ }
1702
+ else {
1703
+ this.applyInlineStyleSmart('fontStyle', 'italic');
1704
+ }
1705
+ this.updateInlineStates();
1706
+ this.takeSnapshot('toggle-italic');
1707
+ }
1708
+ toggleUnderline() {
1709
+ this.focusEditor();
1710
+ this.restoreSelection();
1711
+ const sel = window.getSelection();
1712
+ if (!sel || sel.rangeCount === 0)
1713
+ return;
1714
+ const range = sel.getRangeAt(0);
1715
+ if (range.collapsed)
1716
+ this.expandRangeToWord(range);
1717
+ const anyActive = this.computeUnderlineAnyForRange(range);
1718
+ if (anyActive) {
1719
+ const changed = this.deselectUnderlineBySplitting(range);
1720
+ if (changed) {
1721
+ this.cleanupEmptyUnderlineSpans(this.editorRef.nativeElement);
1722
+ this.emitValue();
1723
+ }
1724
+ else {
1725
+ this.removeInlineStyleInRange(range, 'text-decoration');
1726
+ this.applyInlineStyle('textDecoration', 'none');
1727
+ }
1728
+ }
1729
+ else {
1730
+ this.applyInlineStyleSmart('textDecoration', 'underline');
1731
+ }
1732
+ this.updateInlineStates();
1733
+ this.takeSnapshot('toggle-underline');
1734
+ }
1735
+ getCurrentRange() {
1736
+ const sel = window.getSelection();
1737
+ if (!sel || sel.rangeCount === 0)
1738
+ return null;
1739
+ const range = sel.getRangeAt(0);
1740
+ return this.isRangeInEditor(range) ? range : null;
1741
+ }
1742
+ getEditorChildAncestor(node) {
1743
+ const editor = this.editorRef.nativeElement;
1744
+ let el = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
1745
+ while (el && el.parentElement && el.parentElement !== editor) {
1746
+ el = el.parentElement;
1747
+ }
1748
+ // Wenn direktes Kind des Editors
1749
+ if (el && el.parentElement === editor)
1750
+ return el;
1751
+ return editor;
1752
+ }
1753
+ findClosest(node, selector) {
1754
+ let el = node && node.nodeType === 1 ? node : node?.parentElement ?? null;
1755
+ while (el) {
1756
+ if (el.matches(selector))
1757
+ return el;
1758
+ el = el.parentElement;
1759
+ }
1760
+ return null;
1761
+ }
1762
+ toggleList(kind) {
1763
+ const range = this.getCurrentRange();
1764
+ if (!range)
1765
+ return;
1766
+ const editor = this.editorRef.nativeElement;
1767
+ // Wenn bereits in einer Liste, Liste aufheben
1768
+ const listAncestor = this.findClosest(range.commonAncestorContainer, 'ul,ol');
1769
+ if (listAncestor) {
1770
+ // unwrap: li-Inhalte an die Stelle der Liste setzen
1771
+ const frag = document.createDocumentFragment();
1772
+ const items = Array.from(listAncestor.children); // li
1773
+ for (const li of items) {
1774
+ while (li.firstChild)
1775
+ frag.appendChild(li.firstChild);
1776
+ // Optional: Zeilenumbruch zwischen Items
1777
+ frag.appendChild(document.createElement('br'));
1778
+ }
1779
+ listAncestor.replaceWith(frag);
1780
+ return;
1781
+ }
1782
+ // Neue Liste um die aktuelle Auswahl legen
1783
+ const list = document.createElement(kind);
1784
+ const li = document.createElement('li');
1785
+ const contents = range.extractContents();
1786
+ if (!contents.hasChildNodes()) {
1787
+ li.textContent = '\u200b'; // zero-width space um leere li zu vermeiden
1788
+ }
1789
+ else {
1790
+ li.appendChild(contents);
1791
+ }
1792
+ list.appendChild(li);
1793
+ range.insertNode(list);
1794
+ // Cursor ins li setzen
1795
+ const sel = window.getSelection();
1796
+ if (sel) {
1797
+ sel.removeAllRanges();
1798
+ const after = document.createRange();
1799
+ after.selectNodeContents(li);
1800
+ after.collapse(false);
1801
+ sel.addRange(after);
1802
+ }
1803
+ }
1804
+ alignBlocks(align) {
1805
+ const range = this.getCurrentRange();
1806
+ if (!range)
1807
+ return;
1808
+ const startBlock = this.getEditorChildAncestor(range.startContainer);
1809
+ const endBlock = this.getEditorChildAncestor(range.endContainer);
1810
+ const editor = this.editorRef.nativeElement;
1811
+ const applyAlign = (el) => {
1812
+ if (!el)
1813
+ return;
1814
+ el.style.textAlign = align;
1815
+ };
1816
+ if (startBlock === endBlock) {
1817
+ applyAlign(startBlock);
1818
+ }
1819
+ else {
1820
+ // grob: alle direkten Kinder zwischen start und end ausrichten
1821
+ const children = Array.from(editor.children);
1822
+ const i1 = children.indexOf(startBlock);
1823
+ const i2 = children.indexOf(endBlock);
1824
+ if (i1 >= 0 && i2 >= 0) {
1825
+ const [from, to] = i1 <= i2 ? [i1, i2] : [i2, i1];
1826
+ for (let i = from; i <= to; i++)
1827
+ applyAlign(children[i]);
1828
+ }
1829
+ else {
1830
+ applyAlign(startBlock);
1831
+ }
1832
+ }
1833
+ }
1834
+ setBlockSpacing(kind, target, value) {
1835
+ const range = this.getCurrentRange();
1836
+ if (!range)
1837
+ return;
1838
+ const editor = this.editorRef.nativeElement;
1839
+ const apply = (el) => {
1840
+ if (!el)
1841
+ return;
1842
+ const v = `${value}px`;
1843
+ if (target === 'all') {
1844
+ el.style[kind] = v;
1845
+ }
1846
+ else if (target === 'vertical') {
1847
+ el.style[`${kind}Top`] = v;
1848
+ el.style[`${kind}Bottom`] = v;
1849
+ }
1850
+ else if (target === 'horizontal') {
1851
+ el.style[`${kind}Left`] = v;
1852
+ el.style[`${kind}Right`] = v;
1853
+ }
1854
+ };
1855
+ const startBlock = this.getEditorChildAncestor(range.startContainer);
1856
+ const endBlock = this.getEditorChildAncestor(range.endContainer);
1857
+ if (startBlock === endBlock) {
1858
+ apply(startBlock);
1859
+ }
1860
+ else {
1861
+ const children = Array.from(editor.children);
1862
+ const i1 = children.indexOf(startBlock);
1863
+ const i2 = children.indexOf(endBlock);
1864
+ if (i1 >= 0 && i2 >= 0) {
1865
+ const [from, to] = i1 <= i2 ? [i1, i2] : [i2, i1];
1866
+ for (let i = from; i <= to; i++)
1867
+ apply(children[i]);
1868
+ }
1869
+ else {
1870
+ apply(startBlock);
1871
+ }
1872
+ }
1873
+ }
1874
+ // Versucht, Margin/Padding auf die konkrete Text-Selektion anzuwenden, indem die Auswahl mit einem Span umschlossen wird.
1875
+ // Liefert true, wenn inline angewendet werden konnte; sonst false (z. B. wenn Auswahl blockübergreifend ist).
1876
+ applySpacingToSelection(kind, target, value) {
1877
+ const sel = window.getSelection();
1878
+ if (!sel || sel.rangeCount === 0)
1879
+ return false;
1880
+ const range = sel.getRangeAt(0);
1881
+ if (range.collapsed)
1882
+ return false;
1883
+ const startBlock = this.getEditorChildAncestor(range.startContainer);
1884
+ const endBlock = this.getEditorChildAncestor(range.endContainer);
1885
+ // Nur inline, wenn innerhalb desselben Blocks
1886
+ if (!startBlock || !endBlock || startBlock !== endBlock)
1887
+ return false;
1888
+ const wrapper = document.createElement('span');
1889
+ const px = `${value}px`;
1890
+ if (target === 'all') {
1891
+ wrapper.style[kind] = px;
1892
+ }
1893
+ else if (target === 'vertical') {
1894
+ wrapper.style[`${kind}Top`] = px;
1895
+ wrapper.style[`${kind}Bottom`] = px;
1896
+ }
1897
+ else if (target === 'horizontal') {
1898
+ wrapper.style[`${kind}Left`] = px;
1899
+ wrapper.style[`${kind}Right`] = px;
1900
+ }
1901
+ // Für vertikale Margins auf Inline-Elementen sicherstellen, dass sie greifen
1902
+ if (kind === 'margin' && (target === 'all' || target === 'vertical')) {
1903
+ wrapper.style.display = 'inline-block';
1904
+ }
1905
+ // Auswahl extrahieren und in Wrapper einsetzen
1906
+ const frag = range.extractContents();
1907
+ wrapper.appendChild(frag);
1908
+ range.insertNode(wrapper);
1909
+ // Gleichartige Wrapper zusammenführen und Cursor korrekt setzen
1910
+ const normalized = this.normalizeSpacingSpans(wrapper, kind, target, value) || wrapper;
1911
+ sel.removeAllRanges();
1912
+ const after = document.createRange();
1913
+ if (normalized.isConnected) {
1914
+ after.setStartAfter(normalized);
1915
+ }
1916
+ else {
1917
+ // Fallback: an das Ende des Startblocks
1918
+ const sb = this.getEditorChildAncestor(range.startContainer);
1919
+ if (sb)
1920
+ after.selectNodeContents(sb);
1921
+ }
1922
+ after.collapse(true);
1923
+ sel.addRange(after);
1924
+ return true;
1925
+ }
1926
+ normalizeSpacingSpans(span, kind, target, value) {
1927
+ const props = [];
1928
+ if (target === 'all')
1929
+ props.push(kind);
1930
+ if (target === 'vertical')
1931
+ props.push(`${kind}Top`, `${kind}Bottom`);
1932
+ if (target === 'horizontal')
1933
+ props.push(`${kind}Left`, `${kind}Right`);
1934
+ const displayNeeded = kind === 'margin' && (target === 'all' || target === 'vertical');
1935
+ const v = `${value}px`;
1936
+ const hasSameSpacing = (a, b) => {
1937
+ for (const p of props) {
1938
+ if (a.style[p] !== b.style[p])
1939
+ return false;
1940
+ }
1941
+ if (displayNeeded) {
1942
+ if (a.style.display !== b.style.display)
1943
+ return false;
1944
+ }
1945
+ return true;
1946
+ };
1947
+ // Downward: verschachtelte identische Spans in span zusammenführen
1948
+ if (span.children.length === 1) {
1949
+ const only = span.children[0];
1950
+ if (only && only.tagName === 'SPAN' && hasSameSpacing(span, only)) {
1951
+ while (only.firstChild)
1952
+ span.appendChild(only.firstChild);
1953
+ only.remove();
1954
+ }
1955
+ }
1956
+ let current = span;
1957
+ // Upward: mit Elternelement verschmelzen, wenn identischer Span
1958
+ const parent = current.parentElement;
1959
+ if (parent && parent.tagName === 'SPAN' && hasSameSpacing(parent, current)) {
1960
+ while (current.firstChild)
1961
+ parent.insertBefore(current.firstChild, current);
1962
+ current.remove();
1963
+ current = parent;
1964
+ }
1965
+ if (!current)
1966
+ return null;
1967
+ // Left merge: vorherige Geschwister-Spans mit gleicher Formatierung in current ziehen
1968
+ let prev = current.previousElementSibling;
1969
+ if (prev && prev.tagName === 'SPAN' && hasSameSpacing(prev, current)) {
1970
+ while (current.firstChild)
1971
+ prev.appendChild(current.firstChild);
1972
+ current.remove();
1973
+ current = prev;
1974
+ }
1975
+ // Right merge: folgende Geschwister-Spans in current ziehen
1976
+ let next = current.nextElementSibling;
1977
+ while (next && next.tagName === 'SPAN' && hasSameSpacing(current, next)) {
1978
+ while (next.firstChild)
1979
+ current.appendChild(next.firstChild);
1980
+ const toRemove = next;
1981
+ next = next.nextElementSibling;
1982
+ toRemove.remove();
1983
+ }
1984
+ return current;
1985
+ }
1986
+ setHeading(tag) {
1987
+ const range = this.getCurrentRange();
1988
+ if (!range)
1989
+ return;
1990
+ const editor = this.editorRef.nativeElement;
1991
+ const replaceTag = (el, newTag) => {
1992
+ if (!el || el === editor)
1993
+ return null;
1994
+ const neo = document.createElement(newTag);
1995
+ while (el.firstChild)
1996
+ neo.appendChild(el.firstChild);
1997
+ el.replaceWith(neo);
1998
+ return neo;
1999
+ };
2000
+ const startBlock = this.getEditorChildAncestor(range.startContainer);
2001
+ const endBlock = this.getEditorChildAncestor(range.endContainer);
2002
+ if (startBlock && startBlock !== editor && (startBlock === endBlock)) {
2003
+ const currentTag = (startBlock.tagName || '').toLowerCase();
2004
+ if (currentTag === tag)
2005
+ return;
2006
+ const convertible = /^(p|div|pre|h1|h2|h3|h4|h5|h6)$/i.test(currentTag);
2007
+ if (convertible) {
2008
+ const neo = replaceTag(startBlock, tag);
2009
+ if (neo && tag === 'pre') {
2010
+ neo.setAttribute('style', 'white-space: pre-wrap; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;');
2011
+ }
2012
+ if (neo) {
2013
+ const sel = window.getSelection();
2014
+ if (sel) {
2015
+ sel.removeAllRanges();
2016
+ const after = document.createRange();
2017
+ after.selectNodeContents(neo);
2018
+ after.collapse(false);
2019
+ sel.addRange(after);
2020
+ }
2021
+ }
2022
+ return;
2023
+ }
2024
+ }
2025
+ if (startBlock && endBlock && startBlock !== editor && endBlock !== editor) {
2026
+ const children = Array.from(editor.children);
2027
+ const i1 = children.indexOf(startBlock);
2028
+ const i2 = children.indexOf(endBlock);
2029
+ if (i1 >= 0 && i2 >= 0) {
2030
+ const [from, to] = i1 <= i2 ? [i1, i2] : [i2, i1];
2031
+ let last = null;
2032
+ for (let i = from; i <= to; i++) {
2033
+ const el = children[i];
2034
+ const currentTag = (el.tagName || '').toLowerCase();
2035
+ if (/^(p|div|pre|h1|h2|h3|h4|h5|h6)$/i.test(currentTag)) {
2036
+ const neo = replaceTag(el, tag);
2037
+ if (neo && tag === 'pre') {
2038
+ neo.setAttribute('style', 'white-space: pre-wrap; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;');
2039
+ }
2040
+ if (neo)
2041
+ last = neo;
2042
+ }
2043
+ }
2044
+ if (last) {
2045
+ const sel = window.getSelection();
2046
+ if (sel) {
2047
+ sel.removeAllRanges();
2048
+ const after = document.createRange();
2049
+ after.selectNodeContents(last);
2050
+ after.collapse(false);
2051
+ sel.addRange(after);
2052
+ }
2053
+ }
2054
+ return;
2055
+ }
2056
+ }
2057
+ if (!range.collapsed) {
2058
+ const el = document.createElement(tag);
2059
+ if (tag === 'pre') {
2060
+ el.setAttribute('style', 'white-space: pre-wrap; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;');
2061
+ }
2062
+ const frag = range.extractContents();
2063
+ el.appendChild(frag);
2064
+ range.insertNode(el);
2065
+ const sel = window.getSelection();
2066
+ if (sel) {
2067
+ sel.removeAllRanges();
2068
+ const after = document.createRange();
2069
+ after.selectNodeContents(el);
2070
+ after.collapse(false);
2071
+ sel.addRange(after);
2072
+ }
2073
+ }
2074
+ }
2075
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: StackchRichtextEditorMaterial, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
2076
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.4", type: StackchRichtextEditorMaterial, isStandalone: true, selector: "stackch-richtext-editor-material", inputs: { placeholder: "placeholder", showToolbar: "showToolbar", fonts: "fonts", fontSizes: "fontSizes", height: "height", minHeight: "minHeight", maxHeight: "maxHeight", disabled: "disabled", config: "config" }, outputs: { valueChange: "valueChange" }, host: { listeners: { "document:selectionchange": "onSelectionChange()", "document:click": "closeMenus()", "keydown": "onKeydown($event)" } }, providers: [
2077
+ {
2078
+ provide: NG_VALUE_ACCESSOR,
2079
+ useExisting: forwardRef(() => StackchRichtextEditorMaterial),
2080
+ multi: true,
2081
+ },
2082
+ ], viewQueries: [{ propertyName: "editorRef", first: true, predicate: ["editor"], descendants: true, static: true }], ngImport: i0, template: "<div class=\"stackch_rte\" [class.stackch_rte--disabled]=\"disabled\">\r\n @if (showToolbar) {\r\n <stackch-richtext-editor-material-toolbar\r\n [cfg]=\"cfg\"\r\n [i18n]=\"i18n\"\r\n [disabled]=\"disabled\"\r\n [fonts]=\"fonts\"\r\n [fontSizes]=\"fontSizes\"\r\n [isBoldActive]=\"isBoldActive\"\r\n [isItalicActive]=\"isItalicActive\"\r\n [isUnderlineActive]=\"isUnderlineActive\"\r\n [canUndo]=\"canUndo\"\r\n [canRedo]=\"canRedo\"\r\n [uiState]=\"{ showFontPanel: showFontPanel, showHeadingMenu: showHeadingMenu, showSpacingMenu: showSpacingMenu, showAlignMenu: showAlignMenu, showColorMenu: showColorMenu, showListMenu: showListMenu }\"\r\n\r\n (undo)=\"undo()\"\r\n (redo)=\"redo()\"\r\n (toggleFontPanel)=\"toggleFontPanel($event)\"\r\n (toggleHeadingMenu)=\"toggleHeadingMenu($event)\"\r\n (toggleSpacingMenu)=\"toggleSpacingMenu($event)\"\r\n (toggleAlignMenu)=\"toggleAlignMenu($event)\"\r\n (toggleColorMenu)=\"toggleColorMenu($event)\"\r\n (toggleListMenu)=\"toggleListMenu($event)\"\r\n (pickFont)=\"onPickFont($any($event))\"\r\n (pickFontSize)=\"onPickFontSize($any($event))\"\r\n (pickAlign)=\"onPickAlign($any($event))\"\r\n (pickList)=\"onPickList($any($event))\"\r\n (pickHeading)=\"onPickHeading($any($event))\"\r\n (pickSpacing)=\"onPickSpacing($any($event).kind, $any($event).target, $any($event).value)\"\r\n (applyColor)=\"applyColor($any($event))\"\r\n (applyHighlight)=\"applyHighlight($any($event))\"\r\n (toggleBold)=\"toggleBold()\"\r\n (toggleItalic)=\"toggleItalic()\"\r\n (toggleUnderline)=\"toggleUnderline()\"\r\n (insertLink)=\"insertLink()\"\r\n (removeFormat)=\"removeFormat()\"\r\n (saveSelectionRequest)=\"saveSelection()\"\r\n />\r\n }\r\n\r\n <div #editor\r\n class=\"stackch_rte__editor\"\r\n [attr.contenteditable]=\"!disabled\"\r\n [attr.data-placeholder]=\"placeholder || i18n.placeholder\"\r\n [style.minHeight.px]=\"minHeight\"\r\n [style.maxHeight.px]=\"maxHeight\"\r\n [style.height.px]=\"height\"\r\n (mouseup)=\"saveSelection()\"\r\n (keyup)=\"onKeyup($event)\"\r\n (input)=\"onInput()\"\r\n (blur)=\"onTouched()\"\r\n (paste)=\"onPaste($event)\"></div>\r\n</div>\r\n", styles: [".stackch_rte{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,Arial,sans-serif}.stackch_rte--disabled{opacity:.7;pointer-events:none}.stackch_rte__toolbar{display:flex;flex-wrap:wrap;gap:.25rem;align-items:center;border:1px solid #d0d7de;border-bottom:none;background:#f6f8fa;padding:.25rem;border-radius:6px 6px 0 0}.stackch_rte__btn{appearance:none;border:1px solid #d0d7de;background:#fff;padding:.25rem .5rem;border-radius:4px;cursor:pointer;transition:background .12s ease,border-color .12s ease,color .12s ease;outline:none}.stackch_rte__btn:hover{background:#f3f4f6}.stackch_rte__btn:focus{outline:none;box-shadow:none}.stackch_rte__btn:focus-visible{outline:2px solid rgba(9,105,218,.2)}.stackch_rte__btn.is-active{background:#e7f3ff;border-color:#0969da;color:#084298}.stackch_rte__select{border:1px solid #d0d7de;border-radius:4px;padding:.2rem .4rem;background:#fff}.stackch_rte__color{display:inline-flex;align-items:center;gap:.25rem;border:1px solid #d0d7de;padding:0 .4rem;border-radius:4px;background:#fff}.stackch_rte__color>input{inline-size:1.75rem;block-size:1.5rem;padding:0;border:none;background:none}.stackch_rte__sep{width:1px;height:1.25rem;background:#d0d7de;margin:0 .125rem}.stackch_rte__editor{border:1px solid #d0d7de;border-radius:0 0 6px 6px;padding:.5rem;background:#fff;overflow:auto}.stackch_rte__editor:empty:before{content:attr(data-placeholder);color:#97a1ad;pointer-events:none}.stackch_rte__editor:focus{outline:none;box-shadow:inset 0 0 0 1px #0969da;border-color:#0969da}.stackch_rte__dropdown{position:relative;display:inline-block}.stackch_rte__menu{position:absolute;top:100%;left:0;z-index:2;background:#fff;border:1px solid #d0d7de;border-radius:6px;box-shadow:0 4px 12px #00000014;padding:.25rem;min-width:220px;max-height:220px;overflow:auto}.stackch_rte__menu-item{display:block;width:100%;text-align:left;background:#fff;border:none;padding:.375rem .5rem;border-radius:4px;cursor:pointer}.stackch_rte__menu-item:hover{background:#f3f4f6}.stackch_rte__menu--row{display:flex;flex-direction:row;gap:.25rem;align-items:center;min-width:auto;max-height:none;flex-wrap:nowrap}.stackch_rte__menu--row .stackch_rte__menu-item{display:inline-flex;width:auto;text-align:center;justify-content:center;align-items:center;padding:.3rem .45rem;white-space:nowrap;flex:0 0 auto;word-break:keep-all;overflow-wrap:normal}.stackch_rte__menu--row .stackch_rte__menu-item[style*=\"width:24px\"]{padding:0;width:24px;height:24px}.stackch_rte__menu--grid{display:grid;grid-template-columns:1fr 1fr;gap:.5rem 1rem;min-width:420px}.stackch_rte__menu-section{display:flex;flex-direction:column;gap:.25rem}.stackch_rte__menu-title{font-size:12px;color:#57606a;padding:.25rem;text-transform:uppercase;letter-spacing:.04em}\n"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "component", type: StackchRichtextEditorMaterialToolbar, selector: "stackch-richtext-editor-material-toolbar", inputs: ["cfg", "i18n", "disabled", "fonts", "fontSizes", "isBoldActive", "isItalicActive", "isUnderlineActive", "canUndo", "canRedo", "uiState"], outputs: ["undo", "redo", "toggleFontPanel", "toggleHeadingMenu", "toggleSpacingMenu", "toggleAlignMenu", "toggleColorMenu", "toggleListMenu", "pickFont", "pickFontSize", "pickAlign", "pickList", "pickHeading", "pickSpacing", "applyColor", "applyHighlight", "toggleBold", "toggleItalic", "toggleUnderline", "insertLink", "removeFormat", "saveSelectionRequest"] }] });
2083
+ }
2084
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: StackchRichtextEditorMaterial, decorators: [{
2085
+ type: Component,
2086
+ args: [{ selector: 'stackch-richtext-editor-material', standalone: true, imports: [CommonModule, StackchRichtextEditorMaterialToolbar], providers: [
2087
+ {
2088
+ provide: NG_VALUE_ACCESSOR,
2089
+ useExisting: forwardRef(() => StackchRichtextEditorMaterial),
2090
+ multi: true,
2091
+ },
2092
+ ], template: "<div class=\"stackch_rte\" [class.stackch_rte--disabled]=\"disabled\">\r\n @if (showToolbar) {\r\n <stackch-richtext-editor-material-toolbar\r\n [cfg]=\"cfg\"\r\n [i18n]=\"i18n\"\r\n [disabled]=\"disabled\"\r\n [fonts]=\"fonts\"\r\n [fontSizes]=\"fontSizes\"\r\n [isBoldActive]=\"isBoldActive\"\r\n [isItalicActive]=\"isItalicActive\"\r\n [isUnderlineActive]=\"isUnderlineActive\"\r\n [canUndo]=\"canUndo\"\r\n [canRedo]=\"canRedo\"\r\n [uiState]=\"{ showFontPanel: showFontPanel, showHeadingMenu: showHeadingMenu, showSpacingMenu: showSpacingMenu, showAlignMenu: showAlignMenu, showColorMenu: showColorMenu, showListMenu: showListMenu }\"\r\n\r\n (undo)=\"undo()\"\r\n (redo)=\"redo()\"\r\n (toggleFontPanel)=\"toggleFontPanel($event)\"\r\n (toggleHeadingMenu)=\"toggleHeadingMenu($event)\"\r\n (toggleSpacingMenu)=\"toggleSpacingMenu($event)\"\r\n (toggleAlignMenu)=\"toggleAlignMenu($event)\"\r\n (toggleColorMenu)=\"toggleColorMenu($event)\"\r\n (toggleListMenu)=\"toggleListMenu($event)\"\r\n (pickFont)=\"onPickFont($any($event))\"\r\n (pickFontSize)=\"onPickFontSize($any($event))\"\r\n (pickAlign)=\"onPickAlign($any($event))\"\r\n (pickList)=\"onPickList($any($event))\"\r\n (pickHeading)=\"onPickHeading($any($event))\"\r\n (pickSpacing)=\"onPickSpacing($any($event).kind, $any($event).target, $any($event).value)\"\r\n (applyColor)=\"applyColor($any($event))\"\r\n (applyHighlight)=\"applyHighlight($any($event))\"\r\n (toggleBold)=\"toggleBold()\"\r\n (toggleItalic)=\"toggleItalic()\"\r\n (toggleUnderline)=\"toggleUnderline()\"\r\n (insertLink)=\"insertLink()\"\r\n (removeFormat)=\"removeFormat()\"\r\n (saveSelectionRequest)=\"saveSelection()\"\r\n />\r\n }\r\n\r\n <div #editor\r\n class=\"stackch_rte__editor\"\r\n [attr.contenteditable]=\"!disabled\"\r\n [attr.data-placeholder]=\"placeholder || i18n.placeholder\"\r\n [style.minHeight.px]=\"minHeight\"\r\n [style.maxHeight.px]=\"maxHeight\"\r\n [style.height.px]=\"height\"\r\n (mouseup)=\"saveSelection()\"\r\n (keyup)=\"onKeyup($event)\"\r\n (input)=\"onInput()\"\r\n (blur)=\"onTouched()\"\r\n (paste)=\"onPaste($event)\"></div>\r\n</div>\r\n", styles: [".stackch_rte{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,Arial,sans-serif}.stackch_rte--disabled{opacity:.7;pointer-events:none}.stackch_rte__toolbar{display:flex;flex-wrap:wrap;gap:.25rem;align-items:center;border:1px solid #d0d7de;border-bottom:none;background:#f6f8fa;padding:.25rem;border-radius:6px 6px 0 0}.stackch_rte__btn{appearance:none;border:1px solid #d0d7de;background:#fff;padding:.25rem .5rem;border-radius:4px;cursor:pointer;transition:background .12s ease,border-color .12s ease,color .12s ease;outline:none}.stackch_rte__btn:hover{background:#f3f4f6}.stackch_rte__btn:focus{outline:none;box-shadow:none}.stackch_rte__btn:focus-visible{outline:2px solid rgba(9,105,218,.2)}.stackch_rte__btn.is-active{background:#e7f3ff;border-color:#0969da;color:#084298}.stackch_rte__select{border:1px solid #d0d7de;border-radius:4px;padding:.2rem .4rem;background:#fff}.stackch_rte__color{display:inline-flex;align-items:center;gap:.25rem;border:1px solid #d0d7de;padding:0 .4rem;border-radius:4px;background:#fff}.stackch_rte__color>input{inline-size:1.75rem;block-size:1.5rem;padding:0;border:none;background:none}.stackch_rte__sep{width:1px;height:1.25rem;background:#d0d7de;margin:0 .125rem}.stackch_rte__editor{border:1px solid #d0d7de;border-radius:0 0 6px 6px;padding:.5rem;background:#fff;overflow:auto}.stackch_rte__editor:empty:before{content:attr(data-placeholder);color:#97a1ad;pointer-events:none}.stackch_rte__editor:focus{outline:none;box-shadow:inset 0 0 0 1px #0969da;border-color:#0969da}.stackch_rte__dropdown{position:relative;display:inline-block}.stackch_rte__menu{position:absolute;top:100%;left:0;z-index:2;background:#fff;border:1px solid #d0d7de;border-radius:6px;box-shadow:0 4px 12px #00000014;padding:.25rem;min-width:220px;max-height:220px;overflow:auto}.stackch_rte__menu-item{display:block;width:100%;text-align:left;background:#fff;border:none;padding:.375rem .5rem;border-radius:4px;cursor:pointer}.stackch_rte__menu-item:hover{background:#f3f4f6}.stackch_rte__menu--row{display:flex;flex-direction:row;gap:.25rem;align-items:center;min-width:auto;max-height:none;flex-wrap:nowrap}.stackch_rte__menu--row .stackch_rte__menu-item{display:inline-flex;width:auto;text-align:center;justify-content:center;align-items:center;padding:.3rem .45rem;white-space:nowrap;flex:0 0 auto;word-break:keep-all;overflow-wrap:normal}.stackch_rte__menu--row .stackch_rte__menu-item[style*=\"width:24px\"]{padding:0;width:24px;height:24px}.stackch_rte__menu--grid{display:grid;grid-template-columns:1fr 1fr;gap:.5rem 1rem;min-width:420px}.stackch_rte__menu-section{display:flex;flex-direction:column;gap:.25rem}.stackch_rte__menu-title{font-size:12px;color:#57606a;padding:.25rem;text-transform:uppercase;letter-spacing:.04em}\n"] }]
2093
+ }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }], propDecorators: { placeholder: [{
2094
+ type: Input
2095
+ }], showToolbar: [{
2096
+ type: Input
2097
+ }], fonts: [{
2098
+ type: Input
2099
+ }], fontSizes: [{
2100
+ type: Input
2101
+ }], height: [{
2102
+ type: Input
2103
+ }], minHeight: [{
2104
+ type: Input
2105
+ }], maxHeight: [{
2106
+ type: Input
2107
+ }], disabled: [{
2108
+ type: Input
2109
+ }], valueChange: [{
2110
+ type: Output
2111
+ }], editorRef: [{
2112
+ type: ViewChild,
2113
+ args: ['editor', { static: true }]
2114
+ }], config: [{
2115
+ type: Input
2116
+ }], onSelectionChange: [{
2117
+ type: HostListener,
2118
+ args: ['document:selectionchange']
2119
+ }], closeMenus: [{
2120
+ type: HostListener,
2121
+ args: ['document:click']
2122
+ }], onKeydown: [{
2123
+ type: HostListener,
2124
+ args: ['keydown', ['$event']]
2125
+ }] } });
2126
+
2127
+ /**
2128
+ * Generated bundle index. Do not edit.
2129
+ */
2130
+
2131
+ export { STACKCH_RTE_I18N_DE, STACKCH_RTE_I18N_FR, STACKCH_RTE_I18N_IT, StackchRichtextEditorConfig, StackchRichtextEditorI18n, StackchRichtextEditorMaterial, StackchRichtextEditorMaterialToolbar };
2132
+ //# sourceMappingURL=stackch-angular-material-richtext-editor.mjs.map