Вставить плагин Ckeditor как HTML, а не как текст - PullRequest
0 голосов
/ 03 ноября 2019

Я адаптировал следующий код, чтобы пользователи могли вставлять в Twitter вставку цитаты (блок цитаты). Проблема в том, что он добавляется в виде текста (все символы преобразуются в HTML), а не в HTML. Как я могу исправить свой плагин, чтобы вставить код, такой как HTML, а не текст (без нажатия на источник перед нажатием на иконку моего плагина)?

// Our dialog definition.
CKEDITOR.dialog.add('tweetDialog', function (editor) {
  var lang = editor.lang.tweet;

  return {

    // Basic properties of the dialog window: title, minimum size.
    title: lang.dialogTitle,
    minWidth: 400,
    minHeight: 200,

    // Dialog window contents definition.
    contents: [
    {
      // Definition of the Basic Settings dialog tab (page).
      id: 'tab-basic',
      label: 'Basic Settings',

      // The tab contents.
      elements: [
      {
        // tweet text input field.
        type: 'textarea',
        id: 'text',
        label: lang.dialogTweetText,

        // Validation checking whether the field is not empty.
        validate: CKEDITOR.dialog.validate.notEmpty(lang.dialogTweetTextNotEmpty),

        // Called by the main setupContent call on dialog initialization.
        setup: function (element) {
          var paragraphs = element.find('p');
          if (paragraphs.count() > 0) {
            var tweet = paragraphs.getItem(0).getText();
            for (var i = 1; i < paragraphs.count(); i++) {
              tweet += '\n' + paragraphs.getItem(i).getText();
            }
            this.setValue(tweet);
          }
          else {
            // It is a common blocktweet without <p>.
            this.setValue(element.getText());
          }
        },

        // Called by the main commitContent call on dialog confirmation.
        commit: function (element) {
          // Clear element HTML.
          element.setHtml('');

          // Set a <p> for each line.
          var lines = this.getValue().split(/\r\n|\r|\n/g);
          for (var i = 0; i < lines.length; i++) {
            var p = editor.document.createElement('p');
            p.setText(lines[i]);
            element.append(p);
          }
        }
      },

      ]
    }
    ],

    // Invoked when the dialog is loaded.
    onShow: function () {

      // Get the selection in the editor.
      var selection = editor.getSelection();

      // Get the element at the start of the selection.
      var element = selection.getStartElement();

      // Get the div element closest to the selection, if any.
      if (element) {
        element = element.getAscendant('blocktweet', true);
      }

      // Create a new <div> element if it does not exist.
      if (!element || element.getName() !== 'blocktweet') {
        element = editor.document.createElement('blocktweet');
        // Flag the insertion mode for later use.
        this.insertMode = true;
      }
      else {
        this.insertMode = false;
      }

      // Store the reference to the <div> element in an internal property, for later use.
      this.element = element;

      // Invoke the setup methods of all dialog elements, so they can load the element attributes.
      if (!this.insertMode) {
        this.setupContent(this.element);
      }
    },

    // This method is invoked once a user clicks the OK button, confirming the dialog.
    onOk: function () {

      // The context of this function is the dialog object itself.
      // http://docs.ckeditor.com/#!/api/CKEDITOR.dialog
      var dialog = this;

      var blocktweet = this.element;

      // Invoke the commit methods of all dialog elements, so the <blocktweet> element gets modified.
      this.commitContent(blocktweet);

      // Finally, in if insert mode, inserts the element at the editor caret position.
      if (this.insertMode) {
        editor.insertElement(blocktweet);
      }
    }
  };
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...