plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * @file Configuration helper plugin for CKEditor
  3. * Copyright (C) 2012 Alfonso Martínez de Lizarrondo
  4. *
  5. */
  6. /* global CKEDITOR */
  7. (function () {
  8. "use strict";
  9. // Check if the browser supports the placeholder attribute on textareas natively.
  10. var supportsPlaceholder = "placeholder" in document.createElement("textarea");
  11. // If the data is "empty" (BR, P) or the placeholder then return an empty string.
  12. // Otherwise return the original data
  13. function dataIsEmpty(data) {
  14. if (!data) return true;
  15. if (data.length > 20) return false;
  16. var value = data.replace(/[\n|\t]*/g, "").toLowerCase();
  17. if (
  18. !value ||
  19. value == "<br>" ||
  20. value == "<p>&nbsp;<br></p>" ||
  21. value == "<p><br></p>" ||
  22. value == "<div><br></div>" ||
  23. value == "<p>&nbsp;</p>" ||
  24. value == "&nbsp;" ||
  25. value == " " ||
  26. value == "&nbsp;<br>" ||
  27. value == " <br>"
  28. )
  29. return true;
  30. return false;
  31. }
  32. function addPlaceholder(ev) {
  33. var editor = ev.editor;
  34. // do not add placeholder in readOnly mode
  35. if (editor.readOnly) return;
  36. var root = editor.editable();
  37. var placeholder = ev.listenerData;
  38. if (!root) return;
  39. if (editor.mode == "wysiwyg") {
  40. // If the blur is due to a dialog, don't apply the placeholder
  41. if (CKEDITOR.dialog._.currentTop) return;
  42. if (!root) return;
  43. if (dataIsEmpty(root.getHtml())) {
  44. root.setHtml(placeholder);
  45. root.addClass("placeholder");
  46. }
  47. }
  48. if (editor.mode == "source") {
  49. if (supportsPlaceholder) {
  50. if (ev.name == "mode") {
  51. root.setAttribute("placeholder", placeholder);
  52. }
  53. return;
  54. }
  55. if (dataIsEmpty(root.getValue())) {
  56. root.setValue(placeholder);
  57. root.addClass("placeholder");
  58. }
  59. }
  60. }
  61. function removePlaceholder(ev) {
  62. var editor = ev.editor;
  63. var root = editor.editable();
  64. if (!root) return;
  65. if (editor.mode == "wysiwyg") {
  66. if (!root.hasClass("placeholder")) return;
  67. root.removeClass("placeholder");
  68. // fill it properly
  69. if (CKEDITOR.dtd[root.getName()]["p"]) {
  70. var value = "";
  71. if (editor.enterMode === CKEDITOR.ENTER_P) {
  72. value = "<p><br/></p>";
  73. } else if (editor.enterMode === CKEDITOR.ENTER_DIV) {
  74. value = "<div><br/></div>";
  75. } else {
  76. // This is for CKEDITOR.ENTER_BR
  77. value = "<br/>";
  78. // FireFox prepends an additional line
  79. if (CKEDITOR.env.gecko || CKEDITOR.env.ie) {
  80. value = " ";
  81. }
  82. }
  83. root.setHtml(value);
  84. // Set caret in position
  85. var range = new CKEDITOR.dom.range(editor.document);
  86. range.moveToElementEditablePosition(root.getFirst(), true);
  87. editor.getSelection().selectRanges([range]);
  88. } else {
  89. root.setHtml(" ");
  90. }
  91. }
  92. if (editor.mode == "source") {
  93. if (!root.hasClass("placeholder")) return;
  94. root.removeClass("placeholder");
  95. root.setValue("");
  96. }
  97. }
  98. function handleReadOnlyChange(ev) {
  99. var editor = ev.editor;
  100. if (editor.readOnly) {
  101. removePlaceholder(ev);
  102. } else {
  103. addPlaceholder(ev);
  104. }
  105. }
  106. function getLang(element) {
  107. if (!element) return null;
  108. return element.getAttribute("lang") || getLang(element.getParent());
  109. }
  110. CKEDITOR.plugins.add("confighelper", {
  111. getPlaceholderCss: function () {
  112. return ".placeholder{ color: #999; }";
  113. },
  114. onLoad: function () {
  115. CKEDITOR.addCss(this.getPlaceholderCss());
  116. },
  117. init: function (editor) {
  118. // correct focus status after switch mode
  119. editor.on("mode", function (ev) {
  120. // Let's update to match reality
  121. ev.editor.focusManager.hasFocus = false;
  122. // Now focus it:
  123. });
  124. // Placeholder - Start
  125. // Get the placeholder from the replaced element or from the configuration
  126. var placeholder =
  127. editor.element.getAttribute("placeholder") || editor.config.placeholder;
  128. if (placeholder) {
  129. // CSS for textarea mode
  130. var node = CKEDITOR.document.getHead().append("style");
  131. node.setAttribute("type", "text/css");
  132. var content =
  133. "textarea.placeholder { color: #999; font-style: italic; }";
  134. if (CKEDITOR.env.ie && CKEDITOR.env.version < 11)
  135. node.$.styleSheet.cssText = content;
  136. else node.$.innerHTML = content;
  137. // Watch for the calls to getData to remove the placeholder
  138. editor.on("getData", function (ev) {
  139. var element = editor.editable();
  140. if (element && element.hasClass("placeholder"))
  141. ev.data.dataValue = "";
  142. });
  143. // Watch for setData to remove placeholder class
  144. editor.on("setData", function (ev) {
  145. if (CKEDITOR.dialog._.currentTop) return;
  146. if (editor.mode == "source" && supportsPlaceholder) return;
  147. var root = editor.editable();
  148. if (!root) return;
  149. if (!dataIsEmpty(ev.data.dataValue)) {
  150. // Remove the class if new data is not empty
  151. if (root.hasClass("placeholder")) root.removeClass("placeholder");
  152. } else {
  153. // if data is empty, set it to the placeholder
  154. addPlaceholder(ev);
  155. }
  156. });
  157. editor.on("blur", addPlaceholder, null, placeholder);
  158. editor.on("mode", addPlaceholder, null, placeholder);
  159. editor.on("contentDom", addPlaceholder, null, placeholder);
  160. editor.on("focus", removePlaceholder);
  161. editor.on("key", removePlaceholder);
  162. editor.on("beforeModeUnload", removePlaceholder);
  163. editor.on("readOnly", handleReadOnlyChange, null, placeholder);
  164. } // Placeholder - End
  165. // SCAYT lang from element lang:
  166. var lang = editor.config.contentsLanguage || getLang(editor.element);
  167. if (lang && editor.plugins.scayt && !editor.config.scayt_sLang) {
  168. try {
  169. // Remove the stored language
  170. if (localStorage) localStorage.removeItem("scayt_0_lang");
  171. } catch (e) {
  172. /* */
  173. }
  174. // Convert from HTML5 Lang to spellchecker.net values
  175. var map = {
  176. en: "en_US",
  177. "en-us": "en_US",
  178. "en-gb": "en_GB",
  179. "pt-br": "pt_BR",
  180. da: "da_DK",
  181. "da-dk": "da_DK",
  182. "nl-nl": "nl_NL",
  183. "en-ca": "en_CA",
  184. "fi-fi": "fi_FI",
  185. fr: "fr_FR",
  186. "fr-fr": "fr_FR",
  187. "fr-ca": "fr_CA",
  188. de: "de_DE",
  189. "de-de": "de_DE",
  190. "el-gr": "el_GR",
  191. it: "it_IT",
  192. "it-it": "it_IT",
  193. "nb-no": "nb_NO",
  194. pt: "pt_PT",
  195. "pt-pt": "pt_PT",
  196. es: "es_ES",
  197. "es-es": "es_ES",
  198. "sv-se": "sv_SE",
  199. };
  200. editor.config.scayt_sLang = map[lang.toLowerCase()];
  201. }
  202. // Parse the config to turn it into a js object
  203. // format= dialogName:tabName:fieldName
  204. var parseDefinitionToObject = function (value) {
  205. // Allow JSON definitions
  206. if (typeof value == "object") return value;
  207. var contents = value.split(";"),
  208. tabsToProcess = {},
  209. i;
  210. for (i = 0; i < contents.length; i++) {
  211. var parts = contents[i].split(":");
  212. if (parts.length == 3) {
  213. var dialogName = parts[0],
  214. tabName = parts[1],
  215. fieldName = parts[2];
  216. if (!tabsToProcess[dialogName]) tabsToProcess[dialogName] = {};
  217. if (!tabsToProcess[dialogName][tabName])
  218. tabsToProcess[dialogName][tabName] = [];
  219. tabsToProcess[dialogName][tabName].push(fieldName);
  220. }
  221. }
  222. return tabsToProcess;
  223. };
  224. // Customize dialogs:
  225. function customizeDialogs(ev) {
  226. if (editor != ev.editor) return;
  227. var dialogName = ev.data.name,
  228. dialogDefinition = ev.data.definition,
  229. tabsToProcess,
  230. i,
  231. name,
  232. fields,
  233. tab;
  234. if (dialogName == "tableProperties") dialogName = "table";
  235. // Parse the config to turn it into a js object
  236. if (
  237. !("removeDialogFields" in editor._) &&
  238. editor.config.removeDialogFields
  239. )
  240. editor._.removeDialogFields = parseDefinitionToObject(
  241. editor.config.removeDialogFields
  242. );
  243. // Remove fields of this dialog.
  244. if (
  245. editor._.removeDialogFields &&
  246. (tabsToProcess = editor._.removeDialogFields[dialogName])
  247. ) {
  248. for (name in tabsToProcess) {
  249. fields = tabsToProcess[name];
  250. tab = dialogDefinition.getContents(name);
  251. if (!tab) continue;
  252. for (i = 0; i < fields.length; i++) tab.remove(fields[i]);
  253. }
  254. }
  255. if (!("hideDialogFields" in editor._) && editor.config.hideDialogFields)
  256. editor._.hideDialogFields = parseDefinitionToObject(
  257. editor.config.hideDialogFields
  258. );
  259. // Remove fields of this dialog.
  260. if (
  261. editor._.hideDialogFields &&
  262. (tabsToProcess = editor._.hideDialogFields[dialogName])
  263. ) {
  264. for (name in tabsToProcess) {
  265. fields = tabsToProcess[name];
  266. tab = dialogDefinition.getContents(name);
  267. if (!tab) continue;
  268. for (i = 0; i < fields.length; i++)
  269. tab.get(fields[i]).hidden = true;
  270. }
  271. }
  272. // Set default values.
  273. if (
  274. editor.config.dialogFieldsDefaultValues &&
  275. (tabsToProcess = editor.config.dialogFieldsDefaultValues[dialogName])
  276. ) {
  277. for (name in tabsToProcess) {
  278. fields = tabsToProcess[name];
  279. tab = dialogDefinition.getContents(name);
  280. if (!tab) continue;
  281. for (var fieldName in fields) {
  282. var dialogField = tab.get(fieldName);
  283. if (dialogField) dialogField["default"] = fields[fieldName];
  284. }
  285. }
  286. }
  287. }
  288. CKEDITOR.on("dialogDefinition", customizeDialogs);
  289. editor.once("beforeDestroy", function () {
  290. CKEDITOR.removeListener("dialogDefinition", customizeDialogs);
  291. });
  292. },
  293. });
  294. })();
  295. /**
  296. * Allows to define which dialog fiels must be removed
  297. * @name CKEDITOR.config.removeDialogFields
  298. * @type {String}
  299. * @example
  300. * editor.config.removeDialogFields = "image:info:txtBorder;image:info:txtHSpace";
  301. */
  302. /**
  303. * Allows to define which dialog fiels must be hidden
  304. * @name CKEDITOR.config.hideDialogFields
  305. * @type {String}
  306. * @example
  307. * editor.config.hideDialogFields = "image:info:htmlPreview";
  308. */
  309. /**
  310. * Allows to define default values for dialog fields
  311. * @name CKEDITOR.config.dialogFieldsDefaultValues
  312. * @type {Object}
  313. * @example
  314. config.dialogFieldsDefaultValues =
  315. {
  316. image:
  317. {
  318. advanced:
  319. {
  320. txtGenClass : 'myClass',
  321. txtGenTitle : 'Image title'
  322. }
  323. }
  324. };
  325. */
  326. /**
  327. * Placeholder text for empty editor
  328. * @name CKEDITOR.config.placeholder
  329. * @type {String}
  330. * @example
  331. * editor.config.placeholder = "Please, type here...";
  332. */