Sha256: 6e019c1424be01adcc41e02762775ddf24edef42351f641c1e215becd05c04d6

Contents?: true

Size: 1.96 KB

Versions: 6

Compression:

Stored size: 1.96 KB

Contents

import { Controller } from "@hotwired/stimulus"
import Painterro from "painterro"

export default class extends Controller {
  static targets = ["textarea"]
  connect() {
    const self = this
    this.dirtyTrackingEnabled = false
    this.editor = Painterro({
      id: self.element.id,
      toolbarPosition: "top",
      hiddenTools: ["close", "save"],
      hideByEsc: false,
      saveByEnter: false,
      onImageLoaded: (e) => {
        self.dirtyTrackingEnabled = true
      },
      onChange: (e) => {
        if (self.dirtyTrackingEnabled == false) {
          return
        }
        let event = new CustomEvent("content-editor.changed", {
          bubbles: true,
          cancelable: true,
          detail: {
            contentId: self.data.get("content-id"),
            dirty: true,
          },
        })
        self.element.dispatchEvent(event)
      },
      saveHandler: (image, done) => {
        const formData = new FormData()
        formData.append("_method", "PATCH")
        formData.append(
          "content[data_with_frontmatter]",
          image.asBlob(self.data.get("mime-type"))
        )

        fetch(self.data.get("save-url"), {
          method: "POST",
          headers: {
            Accept: "application/json, text/javascript",
            "X-CSRF-Token": document.querySelector("meta[name=csrf-token]")
              .content,
          },
          body: formData,
        }).then((response) => {
          if (response.status == 200) {
            let event = new CustomEvent("content-editor.changed", {
              bubbles: true,
              cancelable: true,
              detail: {
                contentId: self.data.get("content-id"),
                dirty: false,
              },
            })
            self.element.dispatchEvent(event)
            done(false)
          }
        })
      },
    })
    this.editor.toolByKeyCode = {}

    this.editor.show(this.data.get("url"))
  }
  disconnect() {}
  save() {
    this.editor.save()
  }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
scribo-1.0.44 app/javascript/scribo/controllers/image_editor_controller.js
scribo-1.0.43 app/javascript/scribo/controllers/image_editor_controller.js
scribo-1.0.42 app/javascript/scribo/controllers/image_editor_controller.js
scribo-1.0.41 app/javascript/scribo/controllers/image_editor_controller.js
scribo-1.0.40 app/javascript/scribo/controllers/image_editor_controller.js
scribo-1.0.39 app/javascript/scribo/controllers/image_editor_controller.js