all files / src/ ngx-wig-toolbar.service.ts

28.13% Statements 9/32
0% Branches 0/17
16.67% Functions 1/6
26.67% Lines 8/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                                       
type TButton = {
  title?: string,
  command?: string,
  styleClass?: string,
  pluginName?: string,
  isComplex?: boolean
};
 
type TButtonLibrary = {
  [name: string]: TButton
};
 
export class NgxWigToolbarService {
 
  private _buttonLibrary: TButtonLibrary = {
    list1: {title: 'Unordered List', command: 'insertunorderedlist', styleClass: 'list-ul'},
    list2: {title: 'Ordered List', command: 'insertorderedlist', styleClass: 'list-ol'},
    bold: {title: 'Bold', command: 'bold', styleClass: 'bold'},
    italic: {title: 'Italic', command: 'italic', styleClass: 'italic'},
    link: {title: 'Link', command: 'createlink', styleClass: 'link'}
  };
 
  private _defaultButtonsList = ['list1', 'list2', 'bold', 'italic', 'link'];
 
  public setButtons(buttons: string[]): void {
    // if(!angular.isArray(buttons)) {
    //   throw 'Argument "buttons" should be an array';
    // }
 
    this._defaultButtonsList = buttons;
  };
 
  public addStandardButton(
    name: string,
    title: string,
    command: string,
    styleClass: string
  ) {
 
    if (!name || !title || !command) {
      throw 'Arguments "name", "title" and "command" are required';
    }
 
    styleClass = styleClass || '';
    this._buttonLibrary[name] = {title: title, command: command, styleClass: styleClass};
    this._defaultButtonsList.push(name);
  }
 
  public addCustomButton(name: string, pluginName: string): void {
    if (!name || !pluginName) {
      throw 'Arguments "name" and "pluginName" are required';
    }
 
    this._buttonLibrary[name] = {pluginName: pluginName, isComplex: true};
    this._defaultButtonsList.push(name);
  }
 
  public getToolbarButtons(buttonsList?: string): {}[] {
    let buttons = this._defaultButtonsList;
    const toolbarButtons: TButton[] = [];
 
    if (typeof buttonsList !== 'undefined') {
      buttons = buttonsList.split(',');
    }
 
    buttons.forEach(buttonKey => {
      if (!buttonKey) {
        return;
      }
 
      if (!this._buttonLibrary[buttonKey]) {
        throw 'There is no "' + buttonKey + '" in your library. Possible variants: ' + Object.keys(this._buttonLibrary);
      }
 
      let button = Object.assign({}, this._buttonLibrary[buttonKey]);
      // button.isActive = () => {return !!this.command && document.queryCommandState(this.command);}
      toolbarButtons.push(button);
    });
 
    return toolbarButtons;
  }
 
}