angular 5 和 quilljs [Parchment] 无法创建印迹

标签 angular typescript quill

我正在使用 primeng editor编辑器本身没有任何问题,但是我连续两天都在为自定义标签扩展标准 block 而战,官方文档说要使用任何附加功能quilljs api

我已经检查了 github 上的每个 api 和问题,在我看来,我的方法是正确的,但我无法摆脱这个烦人的错误:

ERROR Error: [Parchment] Unable to create marker blot
at new ParchmentError (scripts.bundle.js:148)
at Object.create (scripts.bundle.js:178)
at BlockBlot.insertAt (scripts.bundle.js:7323)
at Block.insertAt (scripts.bundle.js:855)
at Scroll.ContainerBlot.insertAt (scripts.bundle.js:3404)
at ScrollBlot.insertAt (scripts.bundle.js:7060)
at Scroll.insertAt (scripts.bundle.js:4252)
at Editor.insertEmbed (scripts.bundle.js:2606)
at scripts.bundle.js:1379
at Quill.modify (scripts.bundle.js:1610)

我想要实现的是添加带有不可编辑内容的自定义标签。这是我的代码:

...
import {Editor} from 'primeng/editor';

import * as Quill from 'quill';
import * as Parchment from 'parchment';
const Block = Quill.import('blots/block/embed');
class BlockEmbed extends Parchment.default.Embed {}
BlockEmbed.prototype = Block.prototype;

export class Variable extends BlockEmbed {

  static blotName = 'marker';
  static tagName = 'marker';

  static create(value: any) {
    console.log(value);
    const node = (super.create(value) as any);
    node.innerHTML = '<span contenteditable=false>' + value + '</span>';
    node.setAttribute('contenteditable', false);
    return node;
  }

}

Variable.blotName = 'marker';
Variable.tagName = 'marker';

Quill.register('formats/marker', Variable);

@Component({
  selector: 'manager',
  templateUrl: './manager.component.html',
  styleUrls: ['./manager.component.css']
})

export class ManagerComponent implements OnInit, AfterViewInit {

   private quill: any;
  @ViewChild(Editor) editorComponent: Editor;

  ngOnInit() {}

 // based on primeng github issue this how we can get references to quill 
  ngAfterViewInit() {
    this.quill = this.editorComponent.quill;
  }

 variableSelected(event) {
    // grubbing string variable from event 
    this.quill.insertEmbed(this.cursor.index || 0, 'marker', event.value);
  }

}

基于 quill github 中的这些主题,我的代码应该可以正常工作:

topic 1

topic 2

topic 3

topic 4

那么有人可以帮助我找到我缺少的东西或我的问题所在吗? 提前致谢。

最佳答案

我很高兴用下一种方法解决我的问题

...
declare var Quill: any;
const BlockEmbed = Quill.import('blots/embed');

export class Variable extends BlockEmbed {

  static create(value: any) {
    const node = super.create(typeof value === 'object' ? value.text : value);
    node.innerText = typeof value === 'object' ? value.text : value;
    node.setAttribute('contenteditable', false);
    return node;
  }

  static value(node) {
    return {
      style: node.getAttribute('contenteditable'),
      text: node.innerText
    };
  }

}

Variable['blotName'] = 'marker';
Variable['className'] = 'marker';
Variable['tagName'] = 'span';

Quill.register('formats/marker', Variable);

export class ManagerComponent implements OnInit, AfterViewInit {

  private quill: any;

  @ViewChild('stepper') stepper;
  @ViewChild(Editor) editorComponent: Editor;

...

variableSelected(event) {
    this.quill.insertEmbed(this.cursor.index || 0, 'marker', event.value, 'user');
    this.quill.update('user'); 
  }

关于angular 5 和 quilljs [Parchment] 无法创建印迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48672240/

相关文章:

typescript - 带有 TypeScript 的 AngularJS 2 中使用的括号和特殊字符

javascript - ionic 输入值 [(ngModel)] 没有随着相对组件成员变量的变化而更新

具有多个日期选择的 Angular 6 日历

javascript - 如何使用ng2-charts和chart.js在环外的固定位置绘制极坐标图的外部标签?

Angular2 组件之间的引用

unit-testing - 通过 typescript 使用部分形状进行单元测试

javascript - 如何检索 Quill 文本编辑器的内容

angularjs - 在 angularJS 中,如何将鹅毛笔编辑器的文本字符限制限制为最多 5000 个字符?

javascript - Quill BlockEmbeds 可以使用任意标签吗?

css - 在 Angular 2 中动态更新 css