dropImage.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * DevExtreme (ui/html_editor/modules/dropImage.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 _events_engine = require("../../../events/core/events_engine");
  23. var _events_engine2 = _interopRequireDefault(_events_engine);
  24. var _utils = require("../../../events/utils");
  25. var _iterator = require("../../../core/utils/iterator");
  26. var _browser = require("../../../core/utils/browser");
  27. var _browser2 = _interopRequireDefault(_browser);
  28. var _window = require("../../../core/utils/window");
  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 _getPrototypeOf(o) {
  134. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  135. return o.__proto__ || Object.getPrototypeOf(o)
  136. };
  137. return _getPrototypeOf(o)
  138. }
  139. var BaseModule = (0, _quill_importer.getQuill)().import("core/module");
  140. var DropImageModule = function(_BaseModule) {
  141. _inherits(DropImageModule, _BaseModule);
  142. var _super = _createSuper(DropImageModule);
  143. function DropImageModule(quill, options) {
  144. var _this;
  145. _classCallCheck(this, DropImageModule);
  146. _this = _super.call(this, quill, options);
  147. _this.editorInstance = options.editorInstance;
  148. var widgetName = _this.editorInstance.NAME;
  149. _events_engine2.default.on(_this.quill.root, (0, _utils.addNamespace)("dragover", widgetName), _this._dragOverHandler.bind(_assertThisInitialized(_this)));
  150. _events_engine2.default.on(_this.quill.root, (0, _utils.addNamespace)("drop", widgetName), _this._dropHandler.bind(_assertThisInitialized(_this)));
  151. _events_engine2.default.on(_this.quill.root, (0, _utils.addNamespace)("paste", widgetName), _this._pasteHandler.bind(_assertThisInitialized(_this)));
  152. return _this
  153. }
  154. _createClass(DropImageModule, [{
  155. key: "_dragOverHandler",
  156. value: function(e) {
  157. if (_browser2.default.msie) {
  158. e.preventDefault()
  159. }
  160. }
  161. }, {
  162. key: "_dropHandler",
  163. value: function(e) {
  164. var dataTransfer = e.originalEvent.dataTransfer;
  165. var hasFiles = dataTransfer && dataTransfer.files && dataTransfer.files.length;
  166. e.preventDefault();
  167. if (hasFiles) {
  168. this._getImage(dataTransfer.files, this._addImage.bind(this))
  169. }
  170. }
  171. }, {
  172. key: "_pasteHandler",
  173. value: function(_ref) {
  174. var _this2 = this;
  175. var originalEvent = _ref.originalEvent;
  176. var clipboardData = originalEvent.clipboardData;
  177. if (!clipboardData) {
  178. return
  179. }
  180. var hasDataItems = clipboardData.items && clipboardData.items.length;
  181. var isHtmlData = clipboardData.getData("text/html");
  182. if (!isHtmlData && hasDataItems) {
  183. this._getImage(clipboardData.items, function(imageData) {
  184. if (_browser2.default.mozilla) {
  185. return
  186. }
  187. if (_browser2.default.msie) {
  188. setTimeout(function() {
  189. _this2._addImage(imageData)
  190. })
  191. } else {
  192. _this2._addImage(imageData)
  193. }
  194. })
  195. }
  196. }
  197. }, {
  198. key: "_isImage",
  199. value: function(file) {
  200. return !!file.type.match(/^image\/(a?png|bmp|gif|p?jpe?g|svg|vnd\.microsoft\.icon|webp)/i)
  201. }
  202. }, {
  203. key: "_getImage",
  204. value: function(files, callback) {
  205. var _this3 = this;
  206. var window = (0, _window.getWindow)();
  207. (0, _iterator.each)(files, function(index, file) {
  208. if (!_this3._isImage(file)) {
  209. return
  210. }
  211. var reader = new window.FileReader;
  212. reader.onload = function(_ref2) {
  213. var target = _ref2.target;
  214. callback(target.result)
  215. };
  216. var readableFile = file.getAsFile ? file.getAsFile() : file;
  217. if (readableFile instanceof window.Blob) {
  218. reader.readAsDataURL(readableFile)
  219. }
  220. })
  221. }
  222. }, {
  223. key: "_addImage",
  224. value: function(data) {
  225. var selection = this.quill.getSelection();
  226. var pasteIndex = selection ? selection.index : this.quill.getLength();
  227. this.quill.insertEmbed(pasteIndex, "extendedImage", data, "user")
  228. }
  229. }]);
  230. return DropImageModule
  231. }(BaseModule);
  232. exports.default = DropImageModule;