Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'insertTable' of undefined` error when trying to implement quill-better-table in Angular #70

Open
jsalcedoa opened this issue Jan 7, 2021 · 0 comments

Comments

@jsalcedoa
Copy link

jsalcedoa commented Jan 7, 2021

Hi @soccerloway thank you for creating this addition to quill, I followed a video before this on how to implement quill in my angular project but now I can't implement the quill-better-table to it, I already search and try many ways but no success yet.

This is the error message:

ERROR TypeError: Cannot read property 'insertTable' of undefined

This is my 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>

This is my component TS file:

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);
  }

}

This is my module file:

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 { }

This are my css imports for quill:

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

This are my package.json dependencies for quill:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant