1

我在我的 Angular 应用程序中使用了最新版本的“ngx-editor”,但是当我在内容中使用内联样式div时,它没有正确显示。我的意思是,ngx-editor在所见即所得编辑器中删除了该样式。

我一直在尝试下面的这个例子。

app.component.ts

// my typescript
const content = `
  <div style="background: orange; display: flex; width: 100%; height: 64px">
    Example text with example custom container with inline style
  </div>
  <div style="background: #000000; width: 100%; height: 30px; text-align: center">
    Example footer
  </div>
`
this.formGroup = this.formBuilder.group({
  content: [toDoc(content)],
});

app.component.html

<div class="row">
  <div class="col-12">
    <form [formGroup]="formGroup">
      <ngx-editor formControlName="content"></ngx-editor>
      <button class="btn btn-primary btn-sm" (click)="doSubmit()">Submit</button>
    </form>
  </div>
</div>

但是当我单击提交按钮并控制该结果时,我没有得到那种内联样式。

也许,现在有人如何在 ngx-editor 的内容上使用内联样式。谢谢你的帮助。

4

1 回答 1

2

我希望您使用的是 v5 或 v6 及更高版本。您将需要为此创建自定义架构。

const paragraph: NodeSpec = {
  content: 'inline*',
  group: 'block',
  attrs: {
    align: {
      default: null,
    },
    background: {
      default: null
    }
  },
  parseDOM: [
    {
      tag: 'p',
      getAttrs(dom: HTMLElement): Record<string, any> {
        const { textAlign } = dom.style;
        const align = dom.getAttribute('align') || textAlign || null;

        return {
          align
        };
      }
    },
    {
      tag: 'div', // this will parse div to the editor required format
      getAttrs(dom: HTMLElement): Record<string, any> {
        const { textAlign, background } = dom.style; // you can add required properties here.
        const align = dom.getAttribute('align') || textAlign || null;

        return {
          align,
          background
        };
      }
    }
  ],
  toDOM(node): DOMOutputSpec {
    const { align,background } = node.attrs;

    const styles: Partial<CSSStyleDeclaration> = {
      textAlign: align !== 'left' ? align : null,
      background
    };
    const style = toStyleString(styles) || null;

    return ['p', { style }, 0];
  }
};

您可以根据需要添加/处理其他样式属性。有关更多详细信息,请参阅此处https://github.com/sibiraj-s/ngx-editor/blob/v5.3.0/src/lib/schema/nodes.ts

架构

import { nodes as basicNodes, marks } from 'ngx-editor';

const nodes = Object.assign({}, basicNodes, {
  paragraph,
});

const schema = new Schema({
  nodes,
  marks,
});

对于 v6,在初始化编辑器时

export class AppComponent implements OnInit, OnDestroy {
  editor: Editor;
  ngOnInit(): void {
    this.editor = new Editor({
      schema,
    });
  }

  ngOnDestroy(): void {
    this.editor.destroy();
  }
}

对于 v5,模式创建是相同的。模式应该通过模块定义

NgxEditorModule.forRoot({
  schema
})
于 2021-01-06T14:49:35.837 回答