Няма описание

django-editorjs-fields.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;(function () {
  2. var pluginName = "django_editorjs_fields"
  3. var pluginHelp =
  4. "Write about the issue here: https://github.com/2ik/django-editorjs-fields/issues"
  5. function initEditorJsPlugin() {
  6. var fields = document.querySelectorAll("[data-editorjs-textarea]")
  7. for (let i = 0; i < fields.length; i++) {
  8. initEditorJsField(fields[i])
  9. }
  10. }
  11. function initEditorJsField(textarea) {
  12. if (!textarea) {
  13. logError("bad textarea")
  14. return false
  15. }
  16. var id = textarea.getAttribute("id")
  17. if (!id) {
  18. logError("empty field 'id'")
  19. holder.remove()
  20. return false
  21. }
  22. var holder = document.getElementById(id + "_editorjs_holder")
  23. if (!holder) {
  24. logError("holder not found")
  25. holder.remove()
  26. return false
  27. }
  28. if (id.indexOf("__prefix__") !== -1) return
  29. try {
  30. var config = JSON.parse(textarea.getAttribute("data-config"))
  31. } catch (error) {
  32. console.error(error)
  33. logError(
  34. "invalid 'data-config' on field: " + id + " . Clear the field manually"
  35. )
  36. holder.remove()
  37. return false
  38. }
  39. var text = textarea.value.trim()
  40. if (text) {
  41. try {
  42. text = JSON.parse(text)
  43. } catch (error) {
  44. console.error(error)
  45. logError(
  46. "invalid json data from the database. Clear the field manually"
  47. )
  48. holder.remove()
  49. return false
  50. }
  51. }
  52. textarea.style.display = "none" // remove old textarea
  53. var editorConfig = {
  54. id: id,
  55. holder: holder,
  56. data: text,
  57. }
  58. if ("tools" in config) {
  59. // set config
  60. var tools = config.tools
  61. for (var plugin in tools) {
  62. var cls = tools[plugin].class
  63. if (cls && window[cls] != undefined) {
  64. tools[plugin].class = eval(cls)
  65. continue
  66. }
  67. delete tools[plugin]
  68. logError("[" + plugin + "] Class " + cls + " Not Found")
  69. }
  70. editorConfig.tools = tools
  71. }
  72. if ("autofocus" in config) {
  73. editorConfig.autofocus = !!config.autofocus
  74. }
  75. if ("hideToolbar" in config) {
  76. editorConfig.hideToolbar = !!config.hideToolbar
  77. }
  78. if ("inlineToolbar" in config) {
  79. editorConfig.inlineToolbar = config.inlineToolbar
  80. }
  81. if ("readOnly" in config) {
  82. editorConfig.readOnly = config.readOnly
  83. }
  84. if ("minHeight" in config) {
  85. editorConfig.minHeight = config.minHeight || 300
  86. }
  87. if ("logLevel" in config) {
  88. editorConfig.logLevel = config.logLevel || "VERBOSE"
  89. } else {
  90. editorConfig.logLevel = "ERROR"
  91. }
  92. if ("placeholder" in config) {
  93. editorConfig.placeholder = config.placeholder || "Type text..."
  94. } else {
  95. editorConfig.placeholder = "Type text..."
  96. }
  97. if ("defaultBlock" in config) {
  98. editorConfig.defaultBlock = config.defaultBlock || "paragraph"
  99. }
  100. if ("sanitizer" in config) {
  101. editorConfig.sanitizer = config.sanitizer || {
  102. p: true,
  103. b: true,
  104. a: true,
  105. }
  106. }
  107. if ("i18n" in config) {
  108. editorConfig.i18n = config.i18n || {}
  109. }
  110. editorConfig.onChange = function () {
  111. editor
  112. .save()
  113. .then(function (data) {
  114. if (data.blocks.length) {
  115. textarea.value = JSON.stringify(data)
  116. } else {
  117. textarea.value = 'null'
  118. }
  119. })
  120. .catch(function (error) {
  121. console.log("save error: ", error)
  122. })
  123. }
  124. var editor = new EditorJS(editorConfig)
  125. holder.setAttribute("data-processed", 1)
  126. }
  127. function logError(msg) {
  128. console.error(pluginName + " - " + msg + ". " + pluginHelp)
  129. }
  130. addEventListener("DOMContentLoaded", initEditorJsPlugin)
  131. // Event
  132. if (typeof django === "object" && django.jQuery) {
  133. django.jQuery(document).on("formset:added", function (event, $row) {
  134. var textarea = $row.find("[data-editorjs-textarea]").get(0)
  135. if (textarea) {
  136. initEditorJsField(textarea)
  137. }
  138. })
  139. }
  140. })()