UNPKG

@progress/kendo-angular-editor

Version:
277 lines (276 loc) 11.9 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ import { Component, ViewChild, Input, forwardRef, Output, EventEmitter, ElementRef, Renderer2 } from '@angular/core'; import { NgIf } from '@angular/common'; import { ToolBarToolComponent } from '@progress/kendo-angular-toolbar'; import { DialogService } from '@progress/kendo-angular-dialog'; import { applyFormatIcon } from '@progress/kendo-svg-icons'; import { IconWrapperComponent } from '@progress/kendo-angular-icons'; import { outerWidth, isPresent } from '../../util'; import { EditorLocalizationService } from '../../localization/editor-localization.service'; import { FormatDialogComponent } from '../../dialogs/format-dialog.component'; import { FormatDropDownListComponent } from './editor-format-dropdownlist.component'; import { ProviderService } from '../../common/provider.service'; import { EditorToolsService } from '../tools.service'; import * as i0 from "@angular/core"; import * as i1 from "@progress/kendo-angular-dialog"; import * as i2 from "../../localization/editor-localization.service"; import * as i3 from "../../common/provider.service"; import * as i4 from "../tools.service"; /** * A component which configures an existing `DropDownListComponent` as an Editor tool * ([see example]({% slug toolbartools_editor %}#toc-built-in-tools)). * The component associates a `kendo-dropdownlist` with an Editor command that changes the format of a content block and * automatically defines the options of the drop-down list and sets its values ([see example]({% slug toolbartools_editor %}#toc-dropdownlists)). * * @example * ```ts-no-run * <kendo-toolbar-dropdownlist kendoEditorFormat></kendo-toolbar-dropdownlist> * ``` */ export class EditorFormatComponent extends ToolBarToolComponent { dialogService; localization; toolsService; renderer; value; defaultItem; itemDisabled; disabled = false; tabindex = -1; /** * Overrides the default format items list. */ set data(formatItems) { this._data = formatItems || this._data; } get data() { return this._data; } /** * Fires when the user updates the value of the drop-down list. */ valueChange = new EventEmitter(); formatDropDownList; formatButton; subs; _data = [ { text: 'Paragraph', tag: 'p' }, { text: 'Heading 1', tag: 'h1' }, { text: 'Heading 2', tag: 'h2' }, { text: 'Heading 3', tag: 'h3' }, { text: 'Heading 4', tag: 'h4' }, { text: 'Heading 5', tag: 'h5' }, { text: 'Heading 6', tag: 'h6' } ]; applyFormatSVGIcon = applyFormatIcon; editor; constructor(dialogService, localization, providerService, toolsService, renderer) { super(); this.dialogService = dialogService; this.localization = localization; this.toolsService = toolsService; this.renderer = renderer; this.editor = providerService.editor; this.isBuiltInTool = true; } ngOnInit() { this.itemDisabled = (itemArgs) => { if (!this.overflows && this.formatDropDownList && !this.formatDropDownList.dropDownList.isOpen) { return true; //disable all items in order to prevent navigation when DDL is closed } else { return itemArgs.dataItem.tag === null; } }; setTimeout(() => { this.defaultItem = { text: this.title, tag: null }; this.toolsService.needsCheck.next(); }); this.subs = this.editor.stateChange.subscribe(({ format }) => { const index = this.data.findIndex(item => item.tag === format.selected.tag); this.value = index !== -1 ? format.selected.tag : null; this.disabled = format.disabled; }); } /** * @hidden */ onValueChange(ev) { if (isPresent(ev)) { this.editor.exec('format', { tag: ev }); this.editor.view.focus(); this.valueChange.emit(this.data.find(d => d.tag === ev)); } } ngOnDestroy() { if (this.subs) { this.subs.unsubscribe(); } } get outerWidth() { const element = this.formatDropDownList.element; if (element) { return outerWidth(element.nativeElement); } } get title() { return this.localization.get('format'); } /** * @hidden */ openDialog() { const dialogSettings = { appendTo: this.editor.dialogContainer, content: FormatDialogComponent, width: 400, autoFocusedElement: '.k-picker' }; this.editor.toolbar.toggle(false); const dialogContent = this.dialogService.open(dialogSettings).content.instance; this.renderer.addClass(dialogContent.dialog.dialog.instance.wrapper.nativeElement.querySelector('.k-window'), 'k-editor-window'); dialogContent.setData({ editor: this.editor, data: this.data, defaultItem: this.defaultItem, value: this.value, itemDisabled: this.itemDisabled }); } /** * @hidden */ canFocus() { return !this.disabled; } /** * @hidden */ focus() { this.tabindex = 0; if (this.overflows) { this.formatButton?.nativeElement.focus(); } else { this.formatDropDownList?.focus(); } } /** * @hidden */ handleKey() { this.tabindex = -1; return false; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditorFormatComponent, deps: [{ token: i1.DialogService }, { token: i2.EditorLocalizationService }, { token: i3.ProviderService }, { token: i4.EditorToolsService }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: EditorFormatComponent, isStandalone: true, selector: "kendo-toolbar-dropdownlist[kendoEditorFormat]", inputs: { data: "data" }, outputs: { valueChange: "valueChange" }, providers: [{ provide: ToolBarToolComponent, useExisting: forwardRef(() => EditorFormatComponent) }], viewQueries: [{ propertyName: "formatDropDownList", first: true, predicate: ["formatDropDownList"], descendants: true }, { propertyName: "formatButton", first: true, predicate: ["formatButton"], descendants: true, read: ElementRef }], usesInheritance: true, ngImport: i0, template: ` <ng-template #toolbarTemplate> <kendo-editor-format-dropdownlist #formatDropDownList [defaultItem]="defaultItem" [data]="data" [(value)]="value" [itemDisabled]="itemDisabled" [title]="title" [disabled]="disabled" [tabindex]="tabindex" (valueChange)="onValueChange($event)" > </kendo-editor-format-dropdownlist> </ng-template> <ng-template #popupTemplate> <div #formatButton role="menuitem" class="k-item k-menu-item" [class.k-disabled]="disabled" [tabindex]="tabindex" (click)="openDialog()"> <span class="k-link k-menu-link"> <kendo-icon-wrapper name="apply-format" [svgIcon]="applyFormatSVGIcon"></kendo-icon-wrapper> <span *ngIf="title" class="k-menu-link-text">{{title}}</span> </span> </div> </ng-template> <ng-template #sectionTemplate> <kendo-editor-format-dropdownlist #formatDropDownList [defaultItem]="defaultItem" [data]="data" [(value)]="value" [itemDisabled]="itemDisabled" [title]="title" [disabled]="disabled" [tabindex]="tabindex" (valueChange)="onValueChange($event)" > </kendo-editor-format-dropdownlist> </ng-template> `, isInline: true, dependencies: [{ kind: "component", type: FormatDropDownListComponent, selector: "kendo-editor-format-dropdownlist", inputs: ["data", "value", "defaultItem", "itemDisabled", "title", "disabled", "tabindex"], outputs: ["valueChange"] }, { kind: "component", type: IconWrapperComponent, selector: "kendo-icon-wrapper", inputs: ["name", "svgIcon", "innerCssClass", "customFontClass", "size"], exportAs: ["kendoIconWrapper"] }, { kind: "directive", type: NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditorFormatComponent, decorators: [{ type: Component, args: [{ providers: [{ provide: ToolBarToolComponent, useExisting: forwardRef(() => EditorFormatComponent) }], selector: 'kendo-toolbar-dropdownlist[kendoEditorFormat]', template: ` <ng-template #toolbarTemplate> <kendo-editor-format-dropdownlist #formatDropDownList [defaultItem]="defaultItem" [data]="data" [(value)]="value" [itemDisabled]="itemDisabled" [title]="title" [disabled]="disabled" [tabindex]="tabindex" (valueChange)="onValueChange($event)" > </kendo-editor-format-dropdownlist> </ng-template> <ng-template #popupTemplate> <div #formatButton role="menuitem" class="k-item k-menu-item" [class.k-disabled]="disabled" [tabindex]="tabindex" (click)="openDialog()"> <span class="k-link k-menu-link"> <kendo-icon-wrapper name="apply-format" [svgIcon]="applyFormatSVGIcon"></kendo-icon-wrapper> <span *ngIf="title" class="k-menu-link-text">{{title}}</span> </span> </div> </ng-template> <ng-template #sectionTemplate> <kendo-editor-format-dropdownlist #formatDropDownList [defaultItem]="defaultItem" [data]="data" [(value)]="value" [itemDisabled]="itemDisabled" [title]="title" [disabled]="disabled" [tabindex]="tabindex" (valueChange)="onValueChange($event)" > </kendo-editor-format-dropdownlist> </ng-template> `, standalone: true, imports: [FormatDropDownListComponent, IconWrapperComponent, NgIf] }] }], ctorParameters: function () { return [{ type: i1.DialogService }, { type: i2.EditorLocalizationService }, { type: i3.ProviderService }, { type: i4.EditorToolsService }, { type: i0.Renderer2 }]; }, propDecorators: { data: [{ type: Input }], valueChange: [{ type: Output }], formatDropDownList: [{ type: ViewChild, args: ['formatDropDownList'] }], formatButton: [{ type: ViewChild, args: ['formatButton', { read: ElementRef }] }] } });