Guest User

Untitled

a guest
Apr 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. (def draft-js js/Draft)
  2. ;; or any other mode of import
  3. (def Editor (.-Editor draft-js))
  4. (def EditorState (.-EditorState draft-js))
  5. (def convertToRaw (.-convertToRaw draft-js))
  6. (def convertFromRaw (.-convertFromRaw draft-js))
  7.  
  8. (defn raw-json
  9. [state]
  10. (let [content-state (.getCurrentContent @state)
  11. plain-text (.getPlainText content-state)]
  12. [:div
  13. [:pre
  14. {:style {:height 500}}
  15. (with-out-str (pprint/pprint
  16. (js->clj (convertToRaw content-state))))]
  17. [:div (str plain-text)]]))
  18.  
  19. (defn on-change
  20. [new-editor-state state]
  21. (reset! state new-editor-state))
  22.  
  23. (defn core
  24. [state]
  25. (let []
  26. (r/create-class
  27. {:display-name "core"
  28. :reagent-render (fn []
  29. [:> Editor
  30. {:editorState @state
  31. :onChange (fn [new-editor-state] (on-change new-editor-state state))
  32. }])})))
  33.  
  34. (defn composite-editor
  35. []
  36. (let [state (r/atom (.createEmpty EditorState))]
  37. (fn []
  38. [:div
  39. [core state]
  40. [raw-json state]])))
  41.  
  42. (defn create-with-raw-content
  43. [content]
  44. (let [json (.parse js/JSON content)
  45. content-state (convertFromRaw json)
  46. state (.createWithContent EditorState content-state)]
  47. state))
  48.  
  49. (defn create-from-empty-state
  50. []
  51. (.createEmpty EditorState))
  52.  
  53. (def content
  54. "{\n \"entityMap\": {},\n \"blocks\": [\n {\n \"key\": \"5h45l\",\n \"text\": \"the quick brown fox jumps over the lazy dog \",\n \"type\": \"unstyled\",\n \"depth\": 0,\n \"inlineStyleRanges\": [\n {\n \"offset\": 4,\n \"length\": 5,\n \"style\": \"ITALIC\"\n },\n \n {\n \"offset\": 10,\n \"length\": 5,\n \"style\": \"UNDERLINE\"\n },\n {\n \"offset\": 16,\n \"length\": 3,\n \"style\": \"BOLD\"\n }\n ],\n \"entityRanges\": [],\n \"data\": {}\n }\n ]\n}")
  55.  
  56. (defn composite-editor-raw-content
  57. []
  58. (let [state (r/atom (create-with-raw-content content))
  59. ]
  60. (fn []
  61. [:div
  62. [core state]
  63. [raw-json state]])))
  64.  
  65.  
  66. #_[:div ;; <------------- use
  67. [composite-editor]
  68. [composite-editor-raw-content]]
Add Comment
Please, Sign In to add comment