delta.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**
  2. * DevExtreme (ui/html_editor/converters/delta.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. Object.defineProperty(exports, "__esModule", {
  11. value: true
  12. });
  13. var _converterController = require("../converterController");
  14. var _converterController2 = _interopRequireDefault(_converterController);
  15. var _quill_importer = require("../quill_importer");
  16. var _type = require("../../../core/utils/type");
  17. function _interopRequireDefault(obj) {
  18. return obj && obj.__esModule ? obj : {
  19. "default": obj
  20. }
  21. }
  22. function _toConsumableArray(arr) {
  23. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
  24. }
  25. function _nonIterableSpread() {
  26. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
  27. }
  28. function _arrayWithoutHoles(arr) {
  29. if (Array.isArray(arr)) {
  30. return _arrayLikeToArray(arr)
  31. }
  32. }
  33. function _toArray(arr) {
  34. return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest()
  35. }
  36. function _iterableToArray(iter) {
  37. if ("undefined" !== typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) {
  38. return Array.from(iter)
  39. }
  40. }
  41. function _slicedToArray(arr, i) {
  42. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest()
  43. }
  44. function _nonIterableRest() {
  45. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
  46. }
  47. function _unsupportedIterableToArray(o, minLen) {
  48. if (!o) {
  49. return
  50. }
  51. if ("string" === typeof o) {
  52. return _arrayLikeToArray(o, minLen)
  53. }
  54. var n = Object.prototype.toString.call(o).slice(8, -1);
  55. if ("Object" === n && o.constructor) {
  56. n = o.constructor.name
  57. }
  58. if ("Map" === n || "Set" === n) {
  59. return Array.from(o)
  60. }
  61. if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
  62. return _arrayLikeToArray(o, minLen)
  63. }
  64. }
  65. function _arrayLikeToArray(arr, len) {
  66. if (null == len || len > arr.length) {
  67. len = arr.length
  68. }
  69. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  70. arr2[i] = arr[i]
  71. }
  72. return arr2
  73. }
  74. function _iterableToArrayLimit(arr, i) {
  75. var _i = null == arr ? null : "undefined" !== typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
  76. if (null == _i) {
  77. return
  78. }
  79. var _arr = [];
  80. var _n = true;
  81. var _d = false;
  82. var _s, _e;
  83. try {
  84. for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
  85. _arr.push(_s.value);
  86. if (i && _arr.length === i) {
  87. break
  88. }
  89. }
  90. } catch (err) {
  91. _d = true;
  92. _e = err
  93. } finally {
  94. try {
  95. if (!_n && null != _i.return) {
  96. _i.return()
  97. }
  98. } finally {
  99. if (_d) {
  100. throw _e
  101. }
  102. }
  103. }
  104. return _arr
  105. }
  106. function _arrayWithHoles(arr) {
  107. if (Array.isArray(arr)) {
  108. return arr
  109. }
  110. }
  111. function _classCallCheck(instance, Constructor) {
  112. if (!(instance instanceof Constructor)) {
  113. throw new TypeError("Cannot call a class as a function")
  114. }
  115. }
  116. function _defineProperties(target, props) {
  117. for (var i = 0; i < props.length; i++) {
  118. var descriptor = props[i];
  119. descriptor.enumerable = descriptor.enumerable || false;
  120. descriptor.configurable = true;
  121. if ("value" in descriptor) {
  122. descriptor.writable = true
  123. }
  124. Object.defineProperty(target, descriptor.key, descriptor)
  125. }
  126. }
  127. function _createClass(Constructor, protoProps, staticProps) {
  128. if (protoProps) {
  129. _defineProperties(Constructor.prototype, protoProps)
  130. }
  131. if (staticProps) {
  132. _defineProperties(Constructor, staticProps)
  133. }
  134. Object.defineProperty(Constructor, "prototype", {
  135. writable: false
  136. });
  137. return Constructor
  138. }
  139. var ESCAPING_MAP = {
  140. "&": "&amp;",
  141. "<": "&lt;",
  142. ">": "&gt;",
  143. '"': "&quot;",
  144. "'": "&#39;"
  145. };
  146. var LIST_BLOT_NAME = "list";
  147. var LIST_ITEM_BLOT_NAME = "list-item";
  148. var DeltaConverter = function() {
  149. function DeltaConverter() {
  150. _classCallCheck(this, DeltaConverter);
  151. this.TextBlot = (0, _quill_importer.getQuill)().import("blots/text");
  152. this.BreakBlot = (0, _quill_importer.getQuill)().import("blots/break")
  153. }
  154. _createClass(DeltaConverter, [{
  155. key: "setQuillInstance",
  156. value: function(quillInstance) {
  157. this.quillInstance = quillInstance
  158. }
  159. }, {
  160. key: "toHtml",
  161. value: function() {
  162. if (!this.quillInstance) {
  163. return
  164. }
  165. return this._isQuillEmpty() ? "" : this._convertHTML(this.quillInstance.scroll, 0, this.quillInstance.getLength(), true)
  166. }
  167. }, {
  168. key: "_isQuillEmpty",
  169. value: function() {
  170. var delta = this.quillInstance.getContents();
  171. return 1 === delta.length() && this._isDeltaEmpty(delta)
  172. }
  173. }, {
  174. key: "_isDeltaEmpty",
  175. value: function(delta) {
  176. return delta.reduce(function(__, _ref) {
  177. var insert = _ref.insert;
  178. return insert.indexOf("\n") !== -1
  179. })
  180. }
  181. }, {
  182. key: "_convertHTML",
  183. value: function(blot, index, length) {
  184. var _this = this;
  185. var isRoot = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : false;
  186. if ((0, _type.isFunction)(blot.html)) {
  187. return blot.html(index, length)
  188. }
  189. if (blot instanceof this.TextBlot) {
  190. return this._escapeText(blot.value().slice(index, index + length))
  191. }
  192. if (blot.children) {
  193. if (blot.statics.blotName === LIST_BLOT_NAME) {
  194. return this._convertList(blot, index, length)
  195. }
  196. var parts = [];
  197. blot.children.forEachAt(index, length, function(child, offset, childLength) {
  198. parts.push(_this._convertHTML(child, offset, childLength))
  199. });
  200. this._handleBreakLine(blot.children, parts);
  201. if (isRoot || blot.statics.blotName === LIST_ITEM_BLOT_NAME) {
  202. return parts.join("")
  203. }
  204. var _blot$domNode = blot.domNode,
  205. outerHTML = _blot$domNode.outerHTML,
  206. innerHTML = _blot$domNode.innerHTML;
  207. var _outerHTML$split = outerHTML.split(">".concat(innerHTML, "<")),
  208. _outerHTML$split2 = _slicedToArray(_outerHTML$split, 2),
  209. start = _outerHTML$split2[0],
  210. end = _outerHTML$split2[1];
  211. return "".concat(start, ">").concat(parts.join(""), "<").concat(end)
  212. }
  213. return blot.domNode.outerHTML
  214. }
  215. }, {
  216. key: "_handleBreakLine",
  217. value: function(linkedList, parts) {
  218. if (1 === linkedList.length && linkedList.head instanceof this.BreakBlot) {
  219. parts.push("<br>")
  220. }
  221. }
  222. }, {
  223. key: "_convertList",
  224. value: function(blot, index, length) {
  225. var items = [];
  226. var parentFormats = blot.formats();
  227. blot.children.forEachAt(index, length, function(child, offset, childLength) {
  228. var childFormats = child.formats();
  229. items.push({
  230. child: child,
  231. offset: offset,
  232. length: childLength,
  233. indent: childFormats.indent || 0,
  234. type: parentFormats.list
  235. })
  236. });
  237. return this._getListMarkup(items, -1, [], blot)
  238. }
  239. }, {
  240. key: "_getListMarkup",
  241. value: function(items, lastIndent, listTypes, listBlot) {
  242. if (0 === items.length) {
  243. var _endTag = this._getListType(listTypes.pop());
  244. if (lastIndent <= 0) {
  245. return "</li></".concat(_endTag, ">")
  246. }
  247. return this._processListMarkup([
  248. [], lastIndent - 1, listTypes
  249. ], _endTag)
  250. }
  251. var _items = _toArray(items),
  252. _items$ = _items[0],
  253. child = _items$.child,
  254. offset = _items$.offset,
  255. length = _items$.length,
  256. indent = _items$.indent,
  257. type = _items$.type,
  258. rest = _items.slice(1);
  259. var tag = this._getListType(type);
  260. var childItemArgs = [child, offset, length];
  261. var restItemsArgs = [rest, indent, listTypes];
  262. if (indent > lastIndent) {
  263. listTypes.push(type);
  264. var multiLevelTags = this._correctListMultiIndent(listTypes, type, tag, indent - lastIndent - 1);
  265. return multiLevelTags + this._processIndentListMarkup(childItemArgs, restItemsArgs, tag, listBlot)
  266. }
  267. if (indent === lastIndent) {
  268. return this._processIndentListMarkup(childItemArgs, restItemsArgs)
  269. }
  270. var endTag = this._getListType(listTypes.pop());
  271. return this._processListMarkup([items, lastIndent - 1, listTypes], endTag)
  272. }
  273. }, {
  274. key: "_correctListMultiIndent",
  275. value: function(listTypes, type, tag, indent) {
  276. var markup = "";
  277. while (indent) {
  278. markup += "<".concat(tag, ">");
  279. listTypes.push(type);
  280. indent--
  281. }
  282. return markup
  283. }
  284. }, {
  285. key: "_processListMarkup",
  286. value: function(childItemArgs, tag) {
  287. return "</li></".concat(tag, ">").concat(this._getListMarkup.apply(this, _toConsumableArray(childItemArgs)))
  288. }
  289. }, {
  290. key: "_processIndentListMarkup",
  291. value: function(childItemArgs, restItemsArgs) {
  292. var tag = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "/li";
  293. var listBlot = arguments.length > 3 ? arguments[3] : void 0;
  294. var listAttrs = listBlot && this._getBlotNodeAttributes(listBlot) || "";
  295. var itemAttrs = this._getBlotNodeAttributes(childItemArgs[0]);
  296. return "<".concat(tag).concat(listAttrs, "><li").concat(itemAttrs, ">").concat(this._convertHTML.apply(this, _toConsumableArray(childItemArgs))).concat(this._getListMarkup.apply(this, _toConsumableArray(restItemsArgs).concat([listBlot])))
  297. }
  298. }, {
  299. key: "_getBlotNodeAttributes",
  300. value: function(_ref2) {
  301. var domNode = _ref2.domNode;
  302. if (!domNode.hasAttributes()) {
  303. return ""
  304. }
  305. var attributes = domNode.attributes;
  306. var attributesString = " ";
  307. for (var i = 0; i < attributes.length; i++) {
  308. var name = attributes[i].name;
  309. var value = attributes[i].value;
  310. if ("class" === name) {
  311. value = this._removeIndentClass(value)
  312. }
  313. if (value.length) {
  314. attributesString += "".concat(name, '="').concat(value, '"')
  315. }
  316. }
  317. return attributesString.length > 1 ? attributesString : ""
  318. }
  319. }, {
  320. key: "_getListType",
  321. value: function(type) {
  322. return "ordered" === type ? "ol" : "ul"
  323. }
  324. }, {
  325. key: "_removeIndentClass",
  326. value: function(classString) {
  327. return classString.replace(/ql-indent-\d/g, "").trim()
  328. }
  329. }, {
  330. key: "_escapeText",
  331. value: function(text) {
  332. return text.replace(/[&<>"']/g, function(char) {
  333. return ESCAPING_MAP[char]
  334. })
  335. }
  336. }]);
  337. return DeltaConverter
  338. }();
  339. _converterController2.default.addConverter("delta", DeltaConverter);
  340. exports.default = DeltaConverter;