UNPKG

@progress/kendo-angular-editor

Version:
305 lines (304 loc) 12.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, TemplateRef, ViewChild, ElementRef, forwardRef, EventEmitter, Renderer2 } from '@angular/core'; import { NgIf } from '@angular/common'; import { interval } from 'rxjs'; import { concatMap, take, takeUntil } from 'rxjs/operators'; import { ToolBarToolComponent } from '@progress/kendo-angular-toolbar'; import { PopupService } from '@progress/kendo-angular-popup'; import { DialogService } from '@progress/kendo-angular-dialog'; import { tableAddIcon } from '@progress/kendo-svg-icons'; import { IconWrapperComponent } from '@progress/kendo-angular-icons'; import { ButtonComponent } from '@progress/kendo-angular-buttons'; import { outerWidth } from '../../util'; import { EditorLocalizationService } from '../../localization/editor-localization.service'; import { InsertTableDialogComponent } from '../../dialogs/insert-table-dialog.component'; import { isPresent, Keys } from '@progress/kendo-angular-common'; import { ProviderService } from '../../common/provider.service'; import { PopupTableGridComponent } from './popup-table-grid.component'; import * as i0 from "@angular/core"; import * as i1 from "../../localization/editor-localization.service"; import * as i2 from "@progress/kendo-angular-popup"; import * as i3 from "@progress/kendo-angular-dialog"; import * as i4 from "../../common/provider.service"; const popupWrapperWidth = '190px'; const popupWrapperHeight = '164px'; // Set to '192px' when TableWizard button is added; /** * Represents toolbar component which allows creating and insertig a table in the Editor's content. * * Use this toolbar component to let users create and insert a table in the Editor's content. * * @example * ```html * <kendo-editor> * <kendo-toolbar> * <kendo-editor-insert-table-button></kendo-editor-insert-table-button> * </kendo-toolbar> * </kendo-editor> * ``` */ export class EditorInsertTableButtonComponent extends ToolBarToolComponent { localization; popupService; dialogService; renderer; element; overflowElement; popupGridTemplate; /** * @hidden */ popupRef; /** * @hidden */ disabled; addTableSVGIcon = tableAddIcon; open = false; buttonBlurred = new EventEmitter(); cellClicked = new EventEmitter(); subs; editor; constructor(localization, popupService, dialogService, providerService, renderer) { super(); this.localization = localization; this.popupService = popupService; this.dialogService = dialogService; this.renderer = renderer; this.editor = providerService.editor; this.subs = this.editor.stateChange.subscribe(({ insertTable }) => { this.disabled = insertTable.disabled; }); this.subs.add(this.buttonBlurred.pipe(concatMap(() => interval(200).pipe(take(1), takeUntil(this.cellClicked)))).subscribe(() => { this.toggle(false); })); this.isBuiltInTool = true; } ngOnDestroy() { this.destroyPopup(); if (this.subs) { this.subs.unsubscribe(); } } get outerWidth() { if (this.element) { return outerWidth(this.element.nativeElement); } } get title() { return this.localization.get('insertTable'); } /** * @hidden */ toggle(open) { this.open = open === undefined ? !this.open : open; this.destroyPopup(); if (this.open) { this.createPopup(); } } /** * @hidden */ openDialog() { const dialogSettings = { appendTo: this.editor.dialogContainer, content: InsertTableDialogComponent, width: 400 }; 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 }); } /** * @hidden */ onBlur() { this.tabIndex = -1; this.buttonBlurred.emit(); } /** * @hidden */ onCellClick(args) { this.cellClicked.emit(); this.toggle(false); this.editor.exec('insertTable', args); } /** * @hidden */ canFocus() { return !this.disabled; } /** * @hidden */ focus() { const buttonElement = this.getButton(); if (isPresent(buttonElement)) { this.tabIndex = 0; buttonElement.focus(); } } /** * @hidden */ handleKey(ev) { if (ev.code === Keys.Space || ev.code === Keys.Enter || ev.code === Keys.NumpadEnter) { return true; } this.tabIndex = -1; return false; } createPopup() { const horizontalAlign = this.editor.direction === 'rtl' ? 'right' : 'left'; const anchorPosition = { horizontal: horizontalAlign, vertical: 'bottom' }; const popupPosition = { horizontal: horizontalAlign, vertical: 'top' }; this.popupRef = this.popupService.open({ anchor: this.element, anchorAlign: anchorPosition, animate: true, content: this.popupGridTemplate, popupAlign: popupPosition, popupClass: 'k-ct-popup k-group k-reset k-state-border-up', positionMode: 'absolute' }); const popupWrapper = this.popupRef.popupElement; popupWrapper.style.width = popupWrapperWidth; popupWrapper.style.height = popupWrapperHeight; popupWrapper.setAttribute('dir', this.editor.direction); } destroyPopup() { if (this.popupRef) { this.popupRef.close(); this.popupRef = null; } } getButton() { return (this.overflows ? this.overflowElement : this.element)?.nativeElement; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditorInsertTableButtonComponent, deps: [{ token: i1.EditorLocalizationService }, { token: i2.PopupService }, { token: i3.DialogService }, { token: i4.ProviderService }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: EditorInsertTableButtonComponent, isStandalone: true, selector: "kendo-editor-insert-table-button", providers: [{ provide: ToolBarToolComponent, useExisting: forwardRef(() => EditorInsertTableButtonComponent) }], viewQueries: [{ propertyName: "element", first: true, predicate: ["element"], descendants: true, read: ElementRef }, { propertyName: "overflowElement", first: true, predicate: ["overflowElement"], descendants: true, read: ElementRef }, { propertyName: "popupGridTemplate", first: true, predicate: ["popupGridTemplate"], descendants: true, static: true }], usesInheritance: true, ngImport: i0, template: ` <ng-template #toolbarTemplate> <button type="button" kendoButton class="k-toolbar-button" #element [attr.title]="title" [attr.tabindex]="tabIndex" icon="table-add" [svgIcon]="addTableSVGIcon" [disabled]="disabled" (click)="toggle()" (blur)="onBlur()" ></button> </ng-template> <ng-template #popupTemplate> <div #overflowElement role="menuitem" class="k-item k-menu-item" [class.k-disabled]="disabled" [attr.tabindex]="tabIndex" (click)="openDialog()"> <span class="k-link k-menu-link"> <kendo-icon-wrapper name="table-add" [svgIcon]="addTableSVGIcon"></kendo-icon-wrapper> <span *ngIf="title" class="k-menu-link-text">{{title}}</span> </span> </div> </ng-template> <ng-template #popupGridTemplate> <kendo-popup-table-grid (cellClick)="onCellClick($event)"></kendo-popup-table-grid> </ng-template> <ng-template #sectionTemplate> <button type="button" kendoButton class="k-toolbar-button" #element [attr.title]="title" [attr.tabindex]="tabIndex" icon="table-add" [svgIcon]="addTableSVGIcon" [disabled]="disabled" (click)="toggle()" (blur)="onBlur()" ></button> </ng-template> `, isInline: true, dependencies: [{ kind: "component", type: ButtonComponent, selector: "button[kendoButton]", inputs: ["arrowIcon", "toggleable", "togglable", "selected", "tabIndex", "imageUrl", "iconClass", "icon", "disabled", "size", "rounded", "fillMode", "themeColor", "svgIcon", "primary", "look"], outputs: ["selectedChange", "click"], exportAs: ["kendoButton"] }, { 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"] }, { kind: "component", type: PopupTableGridComponent, selector: "kendo-popup-table-grid", outputs: ["cellClick", "tableWizardClick"] }] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditorInsertTableButtonComponent, decorators: [{ type: Component, args: [{ providers: [{ provide: ToolBarToolComponent, useExisting: forwardRef(() => EditorInsertTableButtonComponent) }], selector: 'kendo-editor-insert-table-button', template: ` <ng-template #toolbarTemplate> <button type="button" kendoButton class="k-toolbar-button" #element [attr.title]="title" [attr.tabindex]="tabIndex" icon="table-add" [svgIcon]="addTableSVGIcon" [disabled]="disabled" (click)="toggle()" (blur)="onBlur()" ></button> </ng-template> <ng-template #popupTemplate> <div #overflowElement role="menuitem" class="k-item k-menu-item" [class.k-disabled]="disabled" [attr.tabindex]="tabIndex" (click)="openDialog()"> <span class="k-link k-menu-link"> <kendo-icon-wrapper name="table-add" [svgIcon]="addTableSVGIcon"></kendo-icon-wrapper> <span *ngIf="title" class="k-menu-link-text">{{title}}</span> </span> </div> </ng-template> <ng-template #popupGridTemplate> <kendo-popup-table-grid (cellClick)="onCellClick($event)"></kendo-popup-table-grid> </ng-template> <ng-template #sectionTemplate> <button type="button" kendoButton class="k-toolbar-button" #element [attr.title]="title" [attr.tabindex]="tabIndex" icon="table-add" [svgIcon]="addTableSVGIcon" [disabled]="disabled" (click)="toggle()" (blur)="onBlur()" ></button> </ng-template> `, standalone: true, imports: [ButtonComponent, IconWrapperComponent, NgIf, PopupTableGridComponent] }] }], ctorParameters: function () { return [{ type: i1.EditorLocalizationService }, { type: i2.PopupService }, { type: i3.DialogService }, { type: i4.ProviderService }, { type: i0.Renderer2 }]; }, propDecorators: { element: [{ type: ViewChild, args: ['element', { read: ElementRef }] }], overflowElement: [{ type: ViewChild, args: ['overflowElement', { read: ElementRef }] }], popupGridTemplate: [{ type: ViewChild, args: ['popupGridTemplate', { static: true }] }] } });