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