goog.editor.plugins
#goog.editor #ClojureScript #Closure_Library
cljs で hello world プラグインを実装する
元ネタ
https://google.github.io/closure-library/source/closure/goog/demos/editor/helloworld.html
code:foo/plugin/hello.cljs
(ns foo.plugin.hello
(:import goog.dom.TagName
goog.editor.Plugin))
(defn CljsHelloWorldPlugin []
(this-as this (.call Plugin this)))
(goog/inherits CljsHelloWorldPlugin Plugin)
(set! (.. CljsHelloWorldPlugin -COMMAND) "+cljsHello")
(set! (.. CljsHelloWorldPlugin -prototype -getTrogClassId)
(fn [] "CljsHelloWorld"))
(set! (.. CljsHelloWorldPlugin -prototype -isSupportedCommand)
(fn cmd
(= cmd (.. CljsHelloWorldPlugin -COMMAND))))
(set! (.. CljsHelloWorldPlugin -prototype -execCommandInternal)
(fn cmd
(this-as this
(let [field (.getFieldObject this)
dom-helper (.getEditableDomHelper field)
range (.getRange field)
;; NOTE: IE以外だと dom-helper ではなく goog.dom で作ったノードも追加できるが
;; IEだと追加に失敗してエラーになるので注意
new-node (.createDom dom-helper (.-SPAN TagName) nil "hello cljs world")]
(.removeContents range)
(.insertNode range new-node false)))))
code:foo/core.cljs
(ns foo.core
(:require foo.plugin.hello :as hello)
(:import goog.editor Field
goog.ui.editor DefaultToolbar ToolbarController ToolbarFactory))
(let [field (Field. "your-editor-id")
toolbar (DefaultToolbar.makeToolbar
#js (ToolbarFactory.makeButton hello/CljsHelloWorldPlugin.COMMAND "hello" "Hello")
(js/document.querySelector "#your-toolbar-id"))]
(doto field
(.registerPlugin (hello/CljsHelloWorldPlugin.)))
(ToolbarController. field toolbar)
(.makeEditable field))