0

quill-better-table を機能させるいくつかの方法を試しましたが成功しませんでした。ほとんどの例は Angular 用ではありません。「未定義のプロパティ 'insertTable' を読み取れません」というエラーが表示されます。以下の私のコードとエラー、助けてください。注: これは、本番環境で使用されるアプリケーションを対象としています。

これは私のHTMLコードです:

<div class="card">
  <div ibmGrid>
    <div ibmRow>
      <div ibmCol>
        <button ibmButton="secondary" size="sm" (click)="onInsertTable()">Add table</button>

        <form [formGroup]="editorForm" (ngSubmit)="onSubmit()">
          <div class="form-group">
            <quill-editor [styles]="editorStyle" [modules]="config" formControlName="editor" (onEditorCreated)="editorCreated($event)"></quill-editor>
          </div>
         
          <button ibmButton="primary">Save changes</button>
        </form>
      </div>
    </div>
</div>

これは私のAngular TSコードです:

 import { Component, OnInit, AfterViewInit, ChangeDetectionStrategy } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { DropdownModule, ModalService } from 'carbon-components-angular';
import * as quillBetterTable from 'quill-better-table';


interface Quill {
  getModule(moduleName: string);
}

interface BetterTableModule {
  insertTable(rows: number, columns: number): void;
}

// declare const require: any;
// let Quill: any = null;


@Component({
  selector: 'app-fnl-report',
  templateUrl: './fnl-report.component.html',
  styleUrls: ['./fnl-report.component.scss'],
  // changeDetection: ChangeDetectionStrategy.OnPush
})
export class FnlReportComponent implements OnInit {

  editorForm: FormGroup
  public quill: Quill;

  editorStyle = {
    height: '500px', 
    backgroundColor: '#fff'
  }

  config = {
    toolbar: [
      ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
      ['blockquote', 'code-block'],
      [{ 'header': 1 }, { 'header': 2 }],               // custom button values
      [{ 'list': 'ordered'}, { 'list': 'bullet' }],
      [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
      [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
      // [{ 'direction': 'rtl' }],                        // text direction
      [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
      [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
      [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
      [{ 'font': [] }],
      [{ 'align': [] }],
      ['clean'],                                        // remove formatting button
      ['link']                                          // link and image, video ['link', 'image', 'video']  
    ]
  }

  constructor() { }

  ngOnInit() {
    this.editorForm = new FormGroup({
      'editor': new FormControl(null)
    });  
  }

  public editorCreated(event: Quill): void {
    this.quill = event;
    // Example on how to add new table to editor
    this.addNewtable();
  }

  private get tableModule(): BetterTableModule {
    return this.quill.getModule("better-table");
  }

  private addNewtable(): void {
    this.tableModule.insertTable(3, 3);
    console.log('Hi');
  }

  onSubmit() {
    console.log(this.editorForm.get('editor').value);
  }

  onInsertTable() {
    // const tableModule = this.quill.getModule('better-table');
    // tableModule.insertTable(3, 3);
  }

}

これは私のモジュールファイルです:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoadingModule, DropdownModule, GridModule, FileUploaderModule, DialogModule, ButtonModule } from 'carbon-components-angular';
import { Help16Module } from '@carbon/icons-angular/lib/help/16';
import { UploadFilesComponent } from './upload-files/upload-files.component';
import { FnlReportComponent } from './fnl-report/fnl-report.component';
import { ReactiveFormsModule } from '@angular/forms';
import { QuillModule, QuillConfig } from "ngx-quill";
import * as Quill from "quill";
import QuillBetterTable from "quill-better-table";


Quill.register(
  {
    "modules/better-table": QuillBetterTable
  },
  true
);

const quillConfig: QuillConfig = {
  modules: {
    table: false, // disable table module
    "better-table": {
      operationMenu: {
        items: {
          unmergeCells: {
            text: "Another unmerge cells name"
          }
        },
        color: {
          colors: ["#fff", "red", "rgb(0, 0, 0)"], // colors in operationMenu
          text: "Background Colors" // subtitle
        }
      }
    },
    keyboard: {
      bindings: QuillBetterTable.keyboardBindings
    }
  }
};


@NgModule({
  declarations: [
    UploadFilesComponent,
    FnlReportComponent
  ],
  imports: [
    CommonModule,
    LoadingModule,
    DropdownModule,
    GridModule,
    ButtonModule,
    FileUploaderModule,
    Help16Module,
    DialogModule,
    ReactiveFormsModule,
    QuillModule.forRoot(quillConfig)
  ],
  providers:[],
  exports: []
})
export class ImsHubModule { }

これはクイルのCSSインポートです:

@import '~quill/dist/quill.core.css';
@import '~quill/dist/quill.bubble.css';
@import '~quill/dist/quill.snow.css';

これは、クイルとクイル テーブルの package.json 依存関係です。

"dependencies": {
    "ngx-quill": "^13.0.1",
    "quill": "^1.3.7",
    "quill-better-table": "^1.2.10"
}

これは私が得ているエラーです:

FnlReportComponent.html:24 ERROR TypeError: Cannot read property 'insertTable' of undefined
4

1 に答える 1

1

残念ながら、ngx-quill ラッパーで quill-better-table を使用することはできません。ngx-quill は依然として quilljs v1 に基づいています。quill-better-table には quilljs v2.0.0-dev.3 が必要です。これについては、要件セクションで読むことができます:こちら

私の場合、単純な貼り付けと読み取りテーブルをどのように実装したかを共有できます。この記事に触発さ れ、カスタム ブロックで作成されました。そして、クイルに要素を追加する正しい方法ではありません。しかし、私たちは内部でエディターを使用しているため、安全に管理されていると確信しています。

  1. 次のように新しい「ブロック埋め込み」を作成します。
import Quill from 'quill';
const BlockEmbed = Quill.import('blots/block/embed');

    export class TableBlockEmbed extends BlockEmbed {

      static blotName = 'TableBlockEmbed';
      static tagName = 'table';

      static create(value) {
        const node = super.create();
        let valueToReturn = value;
        if (!value.includes('assignedTableId')) {
          const tableId = `assignedTableId-${Date.now()}`;
          valueToReturn = value
            .replace('#tableId', `#${tableId}`)
            .replace('table-layout: fixed;', '');
          node.setAttribute('id', tableId);
        } else {
          const existedId = valueToReturn.match(/#assignedTableId-(\d+)/i)[1];
          node.setAttribute('id', `assignedTableId-${existedId}`);
        }
        node.innerHTML = this.transformValue(valueToReturn);
        return node;
      }

      static transformValue(value) {
        let handleArr = value.split('\n');
        handleArr = handleArr.map(e => e.replace(/^[\s]+/, '')
          .replace(/[\s]+$/, ''));
        return handleArr.join('');
      }

      static value(node) {
        return node.innerHTML;
      }
    }
  1. ngx-quill を使用するコンポーネントのコンストラクターで新しい埋め込みブロックの登録を実行します。
constructor() {
    Quill.register(TableBlockEmbed, true);
  }
  1. add on editor 以下のコードを作成しました。(もちろん、ここで必要なスタイルを追加/削除できます。たとえば、margin: 0 auto !important;を追加しました。テーブルを常に中央に配置したいからです):
onEditorCreated(quill: Quill): void {
    quill.clipboard.addMatcher('TABLE', (node, delta) => {
      const Delta = Quill.import('delta');
      const tableTagStyles = node.getAttribute('style');
      return new Delta([
        {
          insert: {
            TableBlockEmbed:
              `<style>#tableId {${tableTagStyles} margin: 0 auto !important; }</style>` + delta.ops[0].insert.TableBlockEmbed
          }
        }
      ]);
    });
  }
  1. いくつかのスタイルも追加しました:
quill-view,
quill-editor{
  ::ng-deep {
    table {
      width: 100%; // if table has no width - then give it by default 100%
      max-width: 100% !important;
      box-sizing: border-box;
    }
  }
}

私はこの解決策が単なる回避策であることを知っていますが、クイル 2 に基づく ngx-quill を待つまでは、少なくともエディター内にテーブルを貼り付ける機能を提供することができました。

例:

オフィスワードのテーブル:

ここに画像の説明を入力

ngx-quill のテーブル:

ここに画像の説明を入力

エクセルの表:

ここに画像の説明を入力

ngx-quill のテーブル:

ここに画像の説明を入力

于 2021-03-22T17:34:58.450 に答える