toolbar.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /**
  2. * DevExtreme (ui/html_editor/modules/toolbar.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 _toolbar = require("../../toolbar");
  25. var _toolbar2 = _interopRequireDefault(_toolbar);
  26. require("../../select_box");
  27. require("../../color_box/color_view");
  28. var _widget_collector = require("./widget_collector");
  29. var _widget_collector2 = _interopRequireDefault(_widget_collector);
  30. var _iterator = require("../../../core/utils/iterator");
  31. var _type = require("../../../core/utils/type");
  32. var _extend = require("../../../core/utils/extend");
  33. var _message = require("../../../localization/message");
  34. var _inflector = require("../../../core/utils/inflector");
  35. var _events_engine = require("../../../events/core/events_engine");
  36. var _events_engine2 = _interopRequireDefault(_events_engine);
  37. var _utils = require("../../../events/utils");
  38. function _interopRequireDefault(obj) {
  39. return obj && obj.__esModule ? obj : {
  40. "default": obj
  41. }
  42. }
  43. function _classCallCheck(instance, Constructor) {
  44. if (!(instance instanceof Constructor)) {
  45. throw new TypeError("Cannot call a class as a function")
  46. }
  47. }
  48. function _defineProperties(target, props) {
  49. for (var i = 0; i < props.length; i++) {
  50. var descriptor = props[i];
  51. descriptor.enumerable = descriptor.enumerable || false;
  52. descriptor.configurable = true;
  53. if ("value" in descriptor) {
  54. descriptor.writable = true
  55. }
  56. Object.defineProperty(target, descriptor.key, descriptor)
  57. }
  58. }
  59. function _createClass(Constructor, protoProps, staticProps) {
  60. if (protoProps) {
  61. _defineProperties(Constructor.prototype, protoProps)
  62. }
  63. if (staticProps) {
  64. _defineProperties(Constructor, staticProps)
  65. }
  66. Object.defineProperty(Constructor, "prototype", {
  67. writable: false
  68. });
  69. return Constructor
  70. }
  71. function _inherits(subClass, superClass) {
  72. if ("function" !== typeof superClass && null !== superClass) {
  73. throw new TypeError("Super expression must either be null or a function")
  74. }
  75. subClass.prototype = Object.create(superClass && superClass.prototype, {
  76. constructor: {
  77. value: subClass,
  78. writable: true,
  79. configurable: true
  80. }
  81. });
  82. Object.defineProperty(subClass, "prototype", {
  83. writable: false
  84. });
  85. if (superClass) {
  86. _setPrototypeOf(subClass, superClass)
  87. }
  88. }
  89. function _setPrototypeOf(o, p) {
  90. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  91. o.__proto__ = p;
  92. return o
  93. };
  94. return _setPrototypeOf(o, p)
  95. }
  96. function _createSuper(Derived) {
  97. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  98. return function() {
  99. var result, Super = _getPrototypeOf(Derived);
  100. if (hasNativeReflectConstruct) {
  101. var NewTarget = _getPrototypeOf(this).constructor;
  102. result = Reflect.construct(Super, arguments, NewTarget)
  103. } else {
  104. result = Super.apply(this, arguments)
  105. }
  106. return _possibleConstructorReturn(this, result)
  107. }
  108. }
  109. function _possibleConstructorReturn(self, call) {
  110. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  111. return call
  112. } else {
  113. if (void 0 !== call) {
  114. throw new TypeError("Derived constructors may only return object or undefined")
  115. }
  116. }
  117. return _assertThisInitialized(self)
  118. }
  119. function _assertThisInitialized(self) {
  120. if (void 0 === self) {
  121. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  122. }
  123. return self
  124. }
  125. function _isNativeReflectConstruct() {
  126. if ("undefined" === typeof Reflect || !Reflect.construct) {
  127. return false
  128. }
  129. if (Reflect.construct.sham) {
  130. return false
  131. }
  132. if ("function" === typeof Proxy) {
  133. return true
  134. }
  135. try {
  136. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  137. return true
  138. } catch (e) {
  139. return false
  140. }
  141. }
  142. function _getPrototypeOf(o) {
  143. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  144. return o.__proto__ || Object.getPrototypeOf(o)
  145. };
  146. return _getPrototypeOf(o)
  147. }
  148. var BaseModule = (0, _quill_importer.getQuill)().import("core/module");
  149. var TOOLBAR_WRAPPER_CLASS = "dx-htmleditor-toolbar-wrapper";
  150. var TOOLBAR_CLASS = "dx-htmleditor-toolbar";
  151. var TOOLBAR_FORMAT_WIDGET_CLASS = "dx-htmleditor-toolbar-format";
  152. var TOOLBAR_SEPARATOR_CLASS = "dx-htmleditor-toolbar-separator";
  153. var TOOLBAR_MENU_SEPARATOR_CLASS = "dx-htmleditor-toolbar-menu-separator";
  154. var ACTIVE_FORMAT_CLASS = "dx-format-active";
  155. var BOX_ITEM_CONTENT_CLASS = "dx-box-item-content";
  156. var ICON_CLASS = "dx-icon";
  157. var SELECTION_CHANGE_EVENT = "selection-change";
  158. var DIALOG_COLOR_CAPTION = "dxHtmlEditor-dialogColorCaption";
  159. var DIALOG_BACKGROUND_CAPTION = "dxHtmlEditor-dialogBackgroundCaption";
  160. var DIALOG_LINK_CAPTION = "dxHtmlEditor-dialogLinkCaption";
  161. var DIALOG_LINK_FIELD_URL = "dxHtmlEditor-dialogLinkUrlField";
  162. var DIALOG_LINK_FIELD_TEXT = "dxHtmlEditor-dialogLinkTextField";
  163. var DIALOG_LINK_FIELD_TARGET = "dxHtmlEditor-dialogLinkTargetField";
  164. var DIALOG_LINK_FIELD_TARGET_CLASS = "dx-formdialog-field-target";
  165. var DIALOG_IMAGE_CAPTION = "dxHtmlEditor-dialogImageCaption";
  166. var DIALOG_IMAGE_FIELD_URL = "dxHtmlEditor-dialogImageUrlField";
  167. var DIALOG_IMAGE_FIELD_ALT = "dxHtmlEditor-dialogImageAltField";
  168. var DIALOG_IMAGE_FIELD_WIDTH = "dxHtmlEditor-dialogImageWidthField";
  169. var DIALOG_IMAGE_FIELD_HEIGHT = "dxHtmlEditor-dialogImageHeightField";
  170. var USER_ACTION = "user";
  171. var SILENT_ACTION = "silent";
  172. var HEADING_TEXT = (0, _message.format)("dxHtmlEditor-heading");
  173. var NORMAL_TEXT = (0, _message.format)("dxHtmlEditor-normalText");
  174. var ToolbarModule = function(_BaseModule) {
  175. _inherits(ToolbarModule, _BaseModule);
  176. var _super = _createSuper(ToolbarModule);
  177. function ToolbarModule(quill, options) {
  178. var _this;
  179. _classCallCheck(this, ToolbarModule);
  180. _this = _super.call(this, quill, options);
  181. _this._editorInstance = options.editorInstance;
  182. _this._toolbarWidgets = new _widget_collector2.default;
  183. _this._formatHandlers = _this._getFormatHandlers();
  184. if ((0, _type.isDefined)(options.items)) {
  185. _this._addCallbacks();
  186. _this._renderToolbar();
  187. _this.quill.on("editor-change", function(eventName) {
  188. var isSelectionChanged = eventName === SELECTION_CHANGE_EVENT;
  189. _this._updateToolbar(isSelectionChanged)
  190. })
  191. }
  192. return _this
  193. }
  194. _createClass(ToolbarModule, [{
  195. key: "_addCallbacks",
  196. value: function() {
  197. this._editorInstance.addCleanCallback(this.clean.bind(this));
  198. this._editorInstance.addContentInitializedCallback(this.updateHistoryWidgets.bind(this))
  199. }
  200. }, {
  201. key: "_updateToolbar",
  202. value: function(isSelectionChanged) {
  203. this.updateFormatWidgets(isSelectionChanged);
  204. this.updateHistoryWidgets()
  205. }
  206. }, {
  207. key: "_getDefaultClickHandler",
  208. value: function(formatName) {
  209. var _this2 = this;
  210. return function(e) {
  211. var formats = _this2.quill.getFormat();
  212. var value = formats[formatName];
  213. var newValue = !((0, _type.isBoolean)(value) ? value : (0, _type.isDefined)(value));
  214. _this2.quill.format(formatName, newValue, USER_ACTION);
  215. _this2._updateFormatWidget(formatName, newValue, formats)
  216. }
  217. }
  218. }, {
  219. key: "_updateFormatWidget",
  220. value: function(formatName, isApplied, formats) {
  221. var widget = this._toolbarWidgets.getByName(formatName);
  222. if (!widget) {
  223. return
  224. }
  225. if (isApplied) {
  226. this._markActiveFormatWidget(formatName, widget, formats)
  227. } else {
  228. this._resetFormatWidget(formatName, widget);
  229. if (Object.prototype.hasOwnProperty.call(formatName)) {
  230. delete formats[formatName]
  231. }
  232. }
  233. this._toggleClearFormatting(isApplied || !(0, _type.isEmptyObject)(formats))
  234. }
  235. }, {
  236. key: "_getFormatHandlers",
  237. value: function() {
  238. var _this3 = this;
  239. return {
  240. clear: function(e) {
  241. var range = _this3.quill.getSelection();
  242. if (range) {
  243. _this3.quill.removeFormat(range);
  244. _this3.updateFormatWidgets()
  245. }
  246. },
  247. link: this._prepareLinkHandler(),
  248. image: this._prepareImageHandler(),
  249. color: this._prepareColorClickHandler("color"),
  250. background: this._prepareColorClickHandler("background"),
  251. orderedList: this._prepareShortcutHandler("list", "ordered"),
  252. bulletList: this._prepareShortcutHandler("list", "bullet"),
  253. alignLeft: this._prepareShortcutHandler("align", "left"),
  254. alignCenter: this._prepareShortcutHandler("align", "center"),
  255. alignRight: this._prepareShortcutHandler("align", "right"),
  256. alignJustify: this._prepareShortcutHandler("align", "justify"),
  257. codeBlock: this._getDefaultClickHandler("code-block"),
  258. undo: function() {
  259. _this3.quill.history.undo()
  260. },
  261. redo: function() {
  262. _this3.quill.history.redo()
  263. },
  264. increaseIndent: function() {
  265. _this3.quill.format("indent", "+1", USER_ACTION)
  266. },
  267. decreaseIndent: function() {
  268. _this3.quill.format("indent", "-1", USER_ACTION)
  269. },
  270. superscript: this._prepareShortcutHandler("script", "super"),
  271. subscript: this._prepareShortcutHandler("script", "sub")
  272. }
  273. }
  274. }, {
  275. key: "_prepareShortcutHandler",
  276. value: function(formatName, shortcutValue) {
  277. var _this4 = this;
  278. return function() {
  279. var formats = _this4.quill.getFormat();
  280. var value = formats[formatName] === shortcutValue ? false : shortcutValue;
  281. _this4.quill.format(formatName, value, USER_ACTION);
  282. _this4.updateFormatWidgets(true)
  283. }
  284. }
  285. }, {
  286. key: "_prepareLinkHandler",
  287. value: function() {
  288. var _this5 = this;
  289. return function() {
  290. _this5.quill.focus();
  291. var selection = _this5.quill.getSelection();
  292. var hasEmbedContent = _this5._hasEmbedContent(selection);
  293. var formats = selection ? _this5.quill.getFormat() : {};
  294. var formData = {
  295. href: formats.link || "",
  296. text: selection && !hasEmbedContent ? _this5.quill.getText(selection) : "",
  297. target: Object.prototype.hasOwnProperty.call(formats, "target") ? !!formats.target : true
  298. };
  299. _this5._editorInstance.formDialogOption("title", (0, _message.format)(DIALOG_LINK_CAPTION));
  300. var promise = _this5._editorInstance.showFormDialog({
  301. formData: formData,
  302. items: _this5._getLinkFormItems(selection)
  303. });
  304. promise.done(function(formData) {
  305. if (selection && !hasEmbedContent) {
  306. var text = formData.text || formData.href;
  307. var index = selection.index,
  308. length = selection.length;
  309. formData.text = void 0;
  310. length && _this5.quill.deleteText(index, length, SILENT_ACTION);
  311. _this5.quill.insertText(index, text, "link", formData, USER_ACTION);
  312. _this5.quill.setSelection(index + text.length, 0, USER_ACTION)
  313. } else {
  314. formData.text = !selection && !formData.text ? formData.href : formData.text;
  315. _this5.quill.format("link", formData, USER_ACTION)
  316. }
  317. });
  318. promise.fail(function() {
  319. _this5.quill.focus()
  320. })
  321. }
  322. }
  323. }, {
  324. key: "_hasEmbedContent",
  325. value: function(selection) {
  326. return !!selection && this.quill.getText(selection).trim().length < selection.length
  327. }
  328. }, {
  329. key: "_getLinkFormItems",
  330. value: function(selection) {
  331. return [{
  332. dataField: "href",
  333. label: {
  334. text: (0, _message.format)(DIALOG_LINK_FIELD_URL)
  335. }
  336. }, {
  337. dataField: "text",
  338. label: {
  339. text: (0, _message.format)(DIALOG_LINK_FIELD_TEXT)
  340. },
  341. visible: !this._hasEmbedContent(selection)
  342. }, {
  343. dataField: "target",
  344. editorType: "dxCheckBox",
  345. editorOptions: {
  346. text: (0, _message.format)(DIALOG_LINK_FIELD_TARGET)
  347. },
  348. cssClass: DIALOG_LINK_FIELD_TARGET_CLASS,
  349. label: {
  350. visible: false
  351. }
  352. }]
  353. }
  354. }, {
  355. key: "_prepareImageHandler",
  356. value: function() {
  357. var _this6 = this;
  358. return function() {
  359. var formData = _this6.quill.getFormat();
  360. var isUpdateDialog = Object.prototype.hasOwnProperty.call(formData, "imageSrc");
  361. var defaultIndex = _this6._defaultPasteIndex;
  362. if (isUpdateDialog) {
  363. var _this6$quill$getForma = _this6.quill.getFormat(defaultIndex - 1, 1),
  364. imageSrc = _this6$quill$getForma.imageSrc;
  365. formData.src = formData.imageSrc;
  366. delete formData.imageSrc;
  367. if (!imageSrc || 0 === defaultIndex) {
  368. _this6.quill.setSelection(defaultIndex + 1, 0, SILENT_ACTION)
  369. }
  370. }
  371. var formatIndex = _this6._embedFormatIndex;
  372. _this6._editorInstance.formDialogOption("title", (0, _message.format)(DIALOG_IMAGE_CAPTION));
  373. var promise = _this6._editorInstance.showFormDialog({
  374. formData: formData,
  375. items: _this6._imageFormItems
  376. });
  377. promise.done(function(formData) {
  378. var index = defaultIndex;
  379. if (isUpdateDialog) {
  380. index = formatIndex;
  381. _this6.quill.deleteText(index, 1, SILENT_ACTION)
  382. }
  383. _this6.quill.insertEmbed(index, "extendedImage", formData, USER_ACTION);
  384. _this6.quill.setSelection(index + 1, 0, USER_ACTION)
  385. }).always(function() {
  386. _this6.quill.focus()
  387. })
  388. }
  389. }
  390. }, {
  391. key: "_embedFormatIndex",
  392. get: function() {
  393. var selection = this.quill.getSelection();
  394. if (selection) {
  395. if (selection.length) {
  396. return selection.index
  397. } else {
  398. return selection.index - 1
  399. }
  400. } else {
  401. return this.quill.getLength()
  402. }
  403. }
  404. }, {
  405. key: "_defaultPasteIndex",
  406. get: function() {
  407. var selection = this.quill.getSelection();
  408. return selection && selection.index || this.quill.getLength()
  409. }
  410. }, {
  411. key: "_imageFormItems",
  412. get: function() {
  413. return [{
  414. dataField: "src",
  415. label: {
  416. text: (0, _message.format)(DIALOG_IMAGE_FIELD_URL)
  417. }
  418. }, {
  419. dataField: "width",
  420. label: {
  421. text: (0, _message.format)(DIALOG_IMAGE_FIELD_WIDTH)
  422. }
  423. }, {
  424. dataField: "height",
  425. label: {
  426. text: (0, _message.format)(DIALOG_IMAGE_FIELD_HEIGHT)
  427. }
  428. }, {
  429. dataField: "alt",
  430. label: {
  431. text: (0, _message.format)(DIALOG_IMAGE_FIELD_ALT)
  432. }
  433. }]
  434. }
  435. }, {
  436. key: "_renderToolbar",
  437. value: function() {
  438. var _this7 = this;
  439. var container = this.options.container || this._getContainer();
  440. this._$toolbar = (0, _renderer2.default)("<div>").addClass(TOOLBAR_CLASS).appendTo(container);
  441. this._$toolbarContainer = (0, _renderer2.default)(container).addClass(TOOLBAR_WRAPPER_CLASS);
  442. _events_engine2.default.on(this._$toolbarContainer, (0, _utils.addNamespace)("mousedown", this._editorInstance.NAME), function(e) {
  443. e.preventDefault()
  444. });
  445. this.toolbarInstance = this._editorInstance._createComponent(this._$toolbar, _toolbar2.default, this.toolbarConfig);
  446. this._editorInstance.on("optionChanged", function(_ref) {
  447. var name = _ref.name;
  448. if ("readOnly" === name || "disabled" === name) {
  449. _this7.toolbarInstance.option("disabled", _this7.isInteractionDisabled)
  450. }
  451. })
  452. }
  453. }, {
  454. key: "toolbarConfig",
  455. get: function() {
  456. return {
  457. dataSource: this._prepareToolbarItems(),
  458. disabled: this.isInteractionDisabled,
  459. menuContainer: this._$toolbarContainer
  460. }
  461. }
  462. }, {
  463. key: "isInteractionDisabled",
  464. get: function() {
  465. return this._editorInstance.option("readOnly") || this._editorInstance.option("disabled")
  466. }
  467. }, {
  468. key: "clean",
  469. value: function() {
  470. this._toolbarWidgets.clear();
  471. if (this._$toolbarContainer) {
  472. this._$toolbarContainer.empty().removeClass(TOOLBAR_WRAPPER_CLASS)
  473. }
  474. }
  475. }, {
  476. key: "repaint",
  477. value: function() {
  478. this.toolbarInstance && this.toolbarInstance.repaint()
  479. }
  480. }, {
  481. key: "_getContainer",
  482. value: function() {
  483. var $container = (0, _renderer2.default)("<div>");
  484. this._editorInstance.$element().prepend($container);
  485. return $container
  486. }
  487. }, {
  488. key: "_prepareToolbarItems",
  489. value: function() {
  490. var _this8 = this;
  491. var resultItems = [];
  492. (0, _iterator.each)(this.options.items, function(index, item) {
  493. var newItem;
  494. if ((0, _type.isObject)(item)) {
  495. newItem = _this8._handleObjectItem(item)
  496. } else {
  497. if ((0, _type.isString)(item)) {
  498. var buttonItemConfig = _this8._prepareButtonItemConfig(item);
  499. newItem = _this8._getToolbarItem(buttonItemConfig)
  500. }
  501. }
  502. if (newItem) {
  503. resultItems.push(newItem)
  504. }
  505. });
  506. return resultItems
  507. }
  508. }, {
  509. key: "_handleObjectItem",
  510. value: function(item) {
  511. if (item.formatName && item.formatValues && this._isAcceptableItem("dxSelectBox")) {
  512. var selectItemConfig = this._prepareSelectItemConfig(item);
  513. return this._getToolbarItem(selectItemConfig)
  514. } else {
  515. if (item.formatName && this._isAcceptableItem("dxButton")) {
  516. var defaultButtonItemConfig = this._prepareButtonItemConfig(item.formatName);
  517. var buttonItemConfig = (0, _extend.extend)(true, defaultButtonItemConfig, item);
  518. return this._getToolbarItem(buttonItemConfig)
  519. } else {
  520. return this._getToolbarItem(item)
  521. }
  522. }
  523. }
  524. }, {
  525. key: "_isAcceptableItem",
  526. value: function(item, acceptableWidgetName) {
  527. return !item.widget || item.widget === acceptableWidgetName
  528. }
  529. }, {
  530. key: "_prepareButtonItemConfig",
  531. value: function(formatName) {
  532. var iconName = "clear" === formatName ? "clearformat" : formatName;
  533. var buttonText = (0, _inflector.titleize)(formatName);
  534. return {
  535. widget: "dxButton",
  536. formatName: formatName,
  537. options: {
  538. hint: buttonText,
  539. text: buttonText,
  540. icon: iconName.toLowerCase(),
  541. onClick: this._formatHandlers[formatName] || this._getDefaultClickHandler(formatName),
  542. stylingMode: "text"
  543. },
  544. showText: "inMenu"
  545. }
  546. }
  547. }, {
  548. key: "_prepareSelectItemConfig",
  549. value: function(item) {
  550. var _this9 = this;
  551. return (0, _extend.extend)(true, {
  552. widget: "dxSelectBox",
  553. formatName: item.formatName,
  554. options: {
  555. stylingMode: "filled",
  556. dataSource: item.formatValues,
  557. placeholder: (0, _inflector.titleize)(item.formatName),
  558. onValueChanged: function(e) {
  559. if (!_this9._isReset) {
  560. _this9.quill.format(item.formatName, e.value, USER_ACTION);
  561. _this9._setValueSilent(e.component, e.value)
  562. }
  563. }
  564. }
  565. }, item)
  566. }
  567. }, {
  568. key: "_prepareColorClickHandler",
  569. value: function(formatName) {
  570. var _this10 = this;
  571. return function() {
  572. var formData = _this10.quill.getFormat();
  573. var caption = "color" === formatName ? DIALOG_COLOR_CAPTION : DIALOG_BACKGROUND_CAPTION;
  574. _this10._editorInstance.formDialogOption("title", (0, _message.format)(caption));
  575. var promise = _this10._editorInstance.showFormDialog({
  576. formData: formData,
  577. items: [{
  578. dataField: formatName,
  579. editorType: "dxColorView",
  580. editorOptions: {
  581. onContentReady: function(e) {
  582. (0, _renderer2.default)(e.element).closest(".".concat(BOX_ITEM_CONTENT_CLASS)).css("flexBasis", "auto")
  583. },
  584. focusStateEnabled: false
  585. },
  586. label: {
  587. visible: false
  588. }
  589. }]
  590. });
  591. promise.done(function(formData) {
  592. _this10.quill.format(formatName, formData[formatName], USER_ACTION)
  593. });
  594. promise.fail(function() {
  595. _this10.quill.focus()
  596. })
  597. }
  598. }
  599. }, {
  600. key: "_getToolbarItem",
  601. value: function(item) {
  602. var _this11 = this;
  603. var baseItem = {
  604. options: {
  605. onInitialized: function(e) {
  606. if (item.formatName) {
  607. e.component.$element().addClass(TOOLBAR_FORMAT_WIDGET_CLASS);
  608. e.component.$element().toggleClass("dx-".concat(item.formatName.toLowerCase(), "-format"), !!item.formatName);
  609. _this11._toolbarWidgets.add(item.formatName, e.component)
  610. }
  611. }
  612. }
  613. };
  614. return (0, _extend.extend)(true, {
  615. location: "before",
  616. locateInMenu: "auto"
  617. }, this._getDefaultConfig(item.formatName), item, baseItem)
  618. }
  619. }, {
  620. key: "_getDefaultItemsConfig",
  621. value: function() {
  622. return {
  623. header: {
  624. options: {
  625. displayExpr: function(item) {
  626. var isHeaderValue = (0, _type.isDefined)(item) && false !== item;
  627. return isHeaderValue ? "".concat(HEADING_TEXT, " ").concat(item) : NORMAL_TEXT
  628. }
  629. }
  630. },
  631. clear: {
  632. options: {
  633. disabled: true
  634. }
  635. },
  636. undo: {
  637. options: {
  638. disabled: true
  639. }
  640. },
  641. redo: {
  642. options: {
  643. disabled: true
  644. }
  645. },
  646. separator: {
  647. template: function(data, index, element) {
  648. (0, _renderer2.default)(element).addClass(TOOLBAR_SEPARATOR_CLASS)
  649. },
  650. menuItemTemplate: function(data, index, element) {
  651. (0, _renderer2.default)(element).addClass(TOOLBAR_MENU_SEPARATOR_CLASS)
  652. }
  653. }
  654. }
  655. }
  656. }, {
  657. key: "_getDefaultConfig",
  658. value: function(formatName) {
  659. return this._getDefaultItemsConfig()[formatName]
  660. }
  661. }, {
  662. key: "updateHistoryWidgets",
  663. value: function() {
  664. var historyModule = this.quill.history;
  665. if (!historyModule) {
  666. return
  667. }
  668. var undoOps = historyModule.stack.undo;
  669. var redoOps = historyModule.stack.redo;
  670. this._updateHistoryWidget(this._toolbarWidgets.getByName("undo"), undoOps);
  671. this._updateHistoryWidget(this._toolbarWidgets.getByName("redo"), redoOps)
  672. }
  673. }, {
  674. key: "_updateHistoryWidget",
  675. value: function(widget, operations) {
  676. if (!widget) {
  677. return
  678. }
  679. widget.option("disabled", !operations.length)
  680. }
  681. }, {
  682. key: "updateFormatWidgets",
  683. value: function(isResetRequired) {
  684. var selection = this.quill.getSelection();
  685. if (!selection) {
  686. return
  687. }
  688. var formats = this.quill.getFormat(selection);
  689. var hasFormats = !(0, _type.isEmptyObject)(formats);
  690. if (!hasFormats || isResetRequired) {
  691. this._resetFormatWidgets()
  692. }
  693. for (var formatName in formats) {
  694. var widgetName = this._getFormatWidgetName(formatName, formats);
  695. var formatWidget = this._toolbarWidgets.getByName(widgetName) || this._toolbarWidgets.getByName(formatName);
  696. if (!formatWidget) {
  697. continue
  698. }
  699. this._markActiveFormatWidget(formatName, formatWidget, formats)
  700. }
  701. this._toggleClearFormatting(hasFormats)
  702. }
  703. }, {
  704. key: "_markActiveFormatWidget",
  705. value: function(name, widget, formats) {
  706. if (this._isColorFormat(name)) {
  707. this._updateColorWidget(name, formats[name])
  708. }
  709. if ("value" in widget.option()) {
  710. this._setValueSilent(widget, formats[name])
  711. } else {
  712. widget.$element().addClass(ACTIVE_FORMAT_CLASS)
  713. }
  714. }
  715. }, {
  716. key: "_toggleClearFormatting",
  717. value: function(hasFormats) {
  718. var clearWidget = this._toolbarWidgets.getByName("clear");
  719. if (clearWidget) {
  720. clearWidget.option("disabled", !hasFormats)
  721. }
  722. }
  723. }, {
  724. key: "_isColorFormat",
  725. value: function(formatName) {
  726. return "color" === formatName || "background" === formatName
  727. }
  728. }, {
  729. key: "_updateColorWidget",
  730. value: function(formatName, color) {
  731. var formatWidget = this._toolbarWidgets.getByName(formatName);
  732. if (!formatWidget) {
  733. return
  734. }
  735. formatWidget.$element().find(".".concat(ICON_CLASS)).css("borderBottomColor", color || "transparent")
  736. }
  737. }, {
  738. key: "_getFormatWidgetName",
  739. value: function(formatName, formats) {
  740. var widgetName;
  741. switch (formatName) {
  742. case "align":
  743. widgetName = formatName + (0, _inflector.titleize)(formats[formatName]);
  744. break;
  745. case "list":
  746. widgetName = formats[formatName] + (0, _inflector.titleize)(formatName);
  747. break;
  748. case "code-block":
  749. widgetName = "codeBlock";
  750. break;
  751. case "script":
  752. widgetName = formats[formatName] + formatName;
  753. break;
  754. case "imageSrc":
  755. widgetName = "image";
  756. break;
  757. default:
  758. widgetName = formatName
  759. }
  760. return widgetName
  761. }
  762. }, {
  763. key: "_setValueSilent",
  764. value: function(widget, value) {
  765. this._isReset = true;
  766. widget.option("value", value);
  767. this._isReset = false
  768. }
  769. }, {
  770. key: "_resetFormatWidgets",
  771. value: function() {
  772. var _this12 = this;
  773. this._toolbarWidgets.each(function(name, widget) {
  774. _this12._resetFormatWidget(name, widget)
  775. })
  776. }
  777. }, {
  778. key: "_resetFormatWidget",
  779. value: function(name, widget) {
  780. widget.$element().removeClass(ACTIVE_FORMAT_CLASS);
  781. if (this._isColorFormat(name)) {
  782. this._updateColorWidget(name)
  783. }
  784. if ("clear" === name) {
  785. widget.option("disabled", true)
  786. }
  787. if ("dxSelectBox" === widget.NAME) {
  788. this._setValueSilent(widget, null)
  789. }
  790. }
  791. }, {
  792. key: "addClickHandler",
  793. value: function(formatName, handler) {
  794. this._formatHandlers[formatName] = handler;
  795. var formatWidget = this._toolbarWidgets.getByName(formatName);
  796. if (formatWidget && "dxButton" === formatWidget.NAME) {
  797. formatWidget.option("onClick", handler)
  798. }
  799. }
  800. }]);
  801. return ToolbarModule
  802. }(BaseModule);
  803. exports.default = ToolbarModule;