Sha256: 63fdba18f42361e24898420b5fe624a00f5c36dfb8739d37681797245decd726

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

import { Controller as PandaCmsController } from "@hotwired/stimulus";

export default class extends PandaCmsController {
  static targets = ["container", "hidden", "toolbar", "form"];

  connect() {
    this.quillInit();
  }

  /**
   * Fire up quill wyswig editor
   */
  quillInit() {
    const quill = new Quill(this.containerTarget, this.quillOption);
    let _this = this;

    // While we type, copy the text to our hidden form field so it can be saved.
    quill.on("text-change", function (delta) {
      _this.hiddenTarget.value = quill.root.innerHTML;
    });

    // Capture focus on and off events
    quill.on("selection-change", function (range, oldRange, source) {
      if (range === null && oldRange !== null) {
        _this.onFocusOut();
      } else if (range !== null && oldRange === null) _this.onFocus();
    });
  }

  /**
   * Fires when the editor receives focus
   */
  onFocus() {
    // Add a border and reveal the toolbar
    this.element.classList.add("border-gray-200");
    this.toolbarTarget.classList.add("opacity-100");
  }

  /**
   * Fires when the editor loses focus
   */
  onFocusOut() {
    // Hide the border and toolbar
    this.element.classList.remove("border-gray-200");
    this.toolbarTarget.classList.remove("opacity-100");

    // Submit the form to save our updates
    this.formTarget.requestSubmit();
  }

  // Quill configuration options
  get quillOption() {
    return {
      modules: {
        toolbar: this.toolbarTarget,
      },
    };
  }
}

Version data entries

13 entries across 8 versions & 1 rubygems

Version Path
panda_cms-0.2.7 public/panda-cms-assets/javascripts/controllers/rich_text_editor_controller.js
panda_cms-0.2.7 app/javascript/controllers/rich_text_editor_controller.js
panda_cms-0.2.6 public/panda-cms-assets/javascripts/controllers/rich_text_editor_controller.js
panda_cms-0.2.6 app/javascript/controllers/rich_text_editor_controller.js
panda_cms-0.2.5 public/panda-cms-assets/javascripts/controllers/rich_text_editor_controller.js
panda_cms-0.2.5 app/javascript/controllers/rich_text_editor_controller.js
panda_cms-0.2.4 app/javascript/controllers/rich_text_editor_controller.js
panda_cms-0.2.4 public/panda-cms-assets/javascripts/controllers/rich_text_editor_controller.js
panda_cms-0.2.3 public/panda-cms-assets/javascripts/controllers/rich_text_editor_controller.js
panda_cms-0.2.3 app/javascript/controllers/rich_text_editor_controller.js
panda_cms-0.2.2 app/javascript/panda_cms/controllers/rich_text_editor_controller.js
panda_cms-0.2.1 app/javascript/panda_cms/controllers/rich_text_editor_controller.js
panda_cms-0.2.0 app/javascript/panda_cms/controllers/rich_text_editor_controller.js