variables.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * DevExtreme (ui/html_editor/modules/variables.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. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. Object.defineProperty(exports, "__esModule", {
  19. value: true
  20. });
  21. var _quill_importer = require("../quill_importer");
  22. var _renderer = require("../../../core/renderer");
  23. var _renderer2 = _interopRequireDefault(_renderer);
  24. var _popup = require("./popup");
  25. var _popup2 = _interopRequireDefault(_popup);
  26. var _variable = require("../formats/variable");
  27. var _variable2 = _interopRequireDefault(_variable);
  28. var _extend = require("../../../core/utils/extend");
  29. function _interopRequireDefault(obj) {
  30. return obj && obj.__esModule ? obj : {
  31. "default": obj
  32. }
  33. }
  34. function _classCallCheck(instance, Constructor) {
  35. if (!(instance instanceof Constructor)) {
  36. throw new TypeError("Cannot call a class as a function")
  37. }
  38. }
  39. function _defineProperties(target, props) {
  40. for (var i = 0; i < props.length; i++) {
  41. var descriptor = props[i];
  42. descriptor.enumerable = descriptor.enumerable || false;
  43. descriptor.configurable = true;
  44. if ("value" in descriptor) {
  45. descriptor.writable = true
  46. }
  47. Object.defineProperty(target, descriptor.key, descriptor)
  48. }
  49. }
  50. function _createClass(Constructor, protoProps, staticProps) {
  51. if (protoProps) {
  52. _defineProperties(Constructor.prototype, protoProps)
  53. }
  54. if (staticProps) {
  55. _defineProperties(Constructor, staticProps)
  56. }
  57. Object.defineProperty(Constructor, "prototype", {
  58. writable: false
  59. });
  60. return Constructor
  61. }
  62. function _inherits(subClass, superClass) {
  63. if ("function" !== typeof superClass && null !== superClass) {
  64. throw new TypeError("Super expression must either be null or a function")
  65. }
  66. subClass.prototype = Object.create(superClass && superClass.prototype, {
  67. constructor: {
  68. value: subClass,
  69. writable: true,
  70. configurable: true
  71. }
  72. });
  73. Object.defineProperty(subClass, "prototype", {
  74. writable: false
  75. });
  76. if (superClass) {
  77. _setPrototypeOf(subClass, superClass)
  78. }
  79. }
  80. function _setPrototypeOf(o, p) {
  81. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  82. o.__proto__ = p;
  83. return o
  84. };
  85. return _setPrototypeOf(o, p)
  86. }
  87. function _createSuper(Derived) {
  88. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  89. return function() {
  90. var result, Super = _getPrototypeOf(Derived);
  91. if (hasNativeReflectConstruct) {
  92. var NewTarget = _getPrototypeOf(this).constructor;
  93. result = Reflect.construct(Super, arguments, NewTarget)
  94. } else {
  95. result = Super.apply(this, arguments)
  96. }
  97. return _possibleConstructorReturn(this, result)
  98. }
  99. }
  100. function _possibleConstructorReturn(self, call) {
  101. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  102. return call
  103. } else {
  104. if (void 0 !== call) {
  105. throw new TypeError("Derived constructors may only return object or undefined")
  106. }
  107. }
  108. return _assertThisInitialized(self)
  109. }
  110. function _assertThisInitialized(self) {
  111. if (void 0 === self) {
  112. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  113. }
  114. return self
  115. }
  116. function _isNativeReflectConstruct() {
  117. if ("undefined" === typeof Reflect || !Reflect.construct) {
  118. return false
  119. }
  120. if (Reflect.construct.sham) {
  121. return false
  122. }
  123. if ("function" === typeof Proxy) {
  124. return true
  125. }
  126. try {
  127. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  128. return true
  129. } catch (e) {
  130. return false
  131. }
  132. }
  133. function _get() {
  134. if ("undefined" !== typeof Reflect && Reflect.get) {
  135. _get = Reflect.get.bind()
  136. } else {
  137. _get = function(target, property, receiver) {
  138. var base = _superPropBase(target, property);
  139. if (!base) {
  140. return
  141. }
  142. var desc = Object.getOwnPropertyDescriptor(base, property);
  143. if (desc.get) {
  144. return desc.get.call(arguments.length < 3 ? target : receiver)
  145. }
  146. return desc.value
  147. }
  148. }
  149. return _get.apply(this, arguments)
  150. }
  151. function _superPropBase(object, property) {
  152. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  153. object = _getPrototypeOf(object);
  154. if (null === object) {
  155. break
  156. }
  157. }
  158. return object
  159. }
  160. function _getPrototypeOf(o) {
  161. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  162. return o.__proto__ || Object.getPrototypeOf(o)
  163. };
  164. return _getPrototypeOf(o)
  165. }
  166. var VARIABLE_FORMAT_CLASS = "dx-variable-format";
  167. var ACTIVE_FORMAT_CLASS = "dx-format-active";
  168. (0, _quill_importer.getQuill)().register({
  169. "formats/variable": _variable2.default
  170. }, true);
  171. var VariableModule = function(_PopupModule) {
  172. _inherits(VariableModule, _PopupModule);
  173. var _super = _createSuper(VariableModule);
  174. function VariableModule(quill, options) {
  175. var _this;
  176. _classCallCheck(this, VariableModule);
  177. _this = _super.call(this, quill, options);
  178. var toolbar = quill.getModule("toolbar");
  179. if (toolbar) {
  180. toolbar.addClickHandler("variable", _this.showPopup.bind(_assertThisInitialized(_this)))
  181. }
  182. quill.keyboard.addBinding({
  183. key: "P",
  184. altKey: true
  185. }, _this.showPopup.bind(_assertThisInitialized(_this)));
  186. _this._popup.on("shown", function(e) {
  187. var $ofElement = (0, _renderer2.default)(e.component.option("position").of);
  188. if ($ofElement.hasClass(VARIABLE_FORMAT_CLASS)) {
  189. $ofElement.addClass(ACTIVE_FORMAT_CLASS)
  190. }
  191. });
  192. return _this
  193. }
  194. _createClass(VariableModule, [{
  195. key: "_getDefaultOptions",
  196. value: function() {
  197. var baseConfig = _get(_getPrototypeOf(VariableModule.prototype), "_getDefaultOptions", this).call(this);
  198. return (0, _extend.extend)(baseConfig, {
  199. escapeChar: ""
  200. })
  201. }
  202. }, {
  203. key: "showPopup",
  204. value: function(event) {
  205. var selection = this.quill.getSelection();
  206. var position = selection ? selection.index : this.quill.getLength();
  207. this.savePosition(position);
  208. this._resetPopupPosition(event, position);
  209. _get(_getPrototypeOf(VariableModule.prototype), "showPopup", this).call(this)
  210. }
  211. }, {
  212. key: "_resetPopupPosition",
  213. value: function(event, position) {
  214. if (event && event.element) {
  215. this._popup.option("position", {
  216. of: event.element,
  217. offset: {
  218. h: 0,
  219. v: 0
  220. },
  221. my: "top center",
  222. at: "bottom center",
  223. collision: "fit"
  224. })
  225. } else {
  226. var mentionBounds = this.quill.getBounds(position);
  227. var rootRect = this.quill.root.getBoundingClientRect();
  228. this._popup.option("position", {
  229. of: this.quill.root,
  230. offset: {
  231. h: mentionBounds.left,
  232. v: mentionBounds.bottom - rootRect.height
  233. },
  234. my: "top center",
  235. at: "bottom left",
  236. collision: "fit flip"
  237. })
  238. }
  239. }
  240. }, {
  241. key: "insertEmbedContent",
  242. value: function(selectionChangedEvent) {
  243. var caretPosition = this.getPosition();
  244. var selectedItem = selectionChangedEvent.component.option("selectedItem");
  245. var variableData = (0, _extend.extend)({}, {
  246. value: selectedItem,
  247. escapeChar: this.options.escapeChar
  248. });
  249. setTimeout(function() {
  250. this.quill.insertEmbed(caretPosition, "variable", variableData);
  251. this.quill.setSelection(caretPosition + 1)
  252. }.bind(this))
  253. }
  254. }]);
  255. return VariableModule
  256. }(_popup2.default);
  257. exports.default = VariableModule;