在Pell编辑器中粘贴html内容并使用Striptags nodejs包格式化

问题描述 投票:2回答:1

在我们的项目中,我们使用pell WYSIWYG文本编辑器,代码如下:

import { moduleInit } from 'gs-components/export/js/_utils';
import pell from 'pell';

class Wysiwyg {
  constructor (element) {
    this._element = element;

    this._store = this._element.querySelector('.input__input')
    this._placeholder = this._element.querySelector('.input__label');
    this._element.appendChild(this._placeholder);


    this._editor = pell.init({
      element: this._element.querySelector('.wysiwyg__editor'),
      defaultParagraphSeparator: 'p',
      onChange: html => this._store.value = html,
      actions: [
        {
          name: 'heading2',
          icon: '<b>Überschrift</b>',
          title: 'Überschrift'
        },
        {
          name: 'paragraph',
          icon: 'Text',
          title: 'Text'
        },
        'bold',
        'italic',
        'underline',
        'olist',
        'ulist'
      ],
      classes: {
        button: 'gs-btn gs-btn--xs gs-btn--bordered',
        selected: 'gs-btn--dark'
      }
    });

    this._editor.content.innerHTML = this._store.value;

    const pellContent = this._element.querySelector('.pell-content');
    pellContent.addEventListener('focus', (e) => {
      this._store.dispatchEvent(this._buildEvent('focus'));
    });

    pellContent.addEventListener('blur', (e) => {
      this._store.dispatchEvent(this._buildEvent('blur'));
    });

    this._store.dispatchEvent(this._buildEvent('blur'));
  }

  _buildEvent(type) {
    const event = document.createEvent('HTMLEvents');
    event.initEvent(type, true, true);
    return event;
  }
}

export const init = () => moduleInit('.input--wysiwyg', element => {
  new Wysiwyg(element);
});

我们必须实现粘贴事件和striptags功能:

Example

var striptags = require('striptags');

var html =
    '<a href="https://example.com">' +
        'lorem ipsum <strong>dolor</strong> <em>sit</em> amet' +
    '</a>';

striptags(html);
striptags(html, '<strong>');
striptags(html, ['a']);
striptags(html, [], '\n');

但是,如何以及在哪里?

javascript node.js gruntjs wysiwyg dom-events
1个回答
1
投票

这是答案:

pellContent.addEventListener('paste', (e) => {
  setTimeout(() => {
    this._editor.content.innerHTML = striptags(this._editor.content.innerHTML, ['h2', 'p', 'br', 'ul', 'ol', 'li']);
    this._store.value = this._editor.content.innerHTML;
  })
});
© www.soinside.com 2019 - 2024. All rights reserved.