index.umd.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@antv/g-lite')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@antv/g-lite'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.G = global.G || {}, global.G.HTMLRenderer = {}), global.window.G));
  5. }(this, (function (exports, gLite) { 'use strict';
  6. function _inheritsLoose(subClass, superClass) {
  7. subClass.prototype = Object.create(superClass.prototype);
  8. subClass.prototype.constructor = subClass;
  9. _setPrototypeOf(subClass, superClass);
  10. }
  11. function _setPrototypeOf(o, p) {
  12. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
  13. o.__proto__ = p;
  14. return o;
  15. };
  16. return _setPrototypeOf(o, p);
  17. }
  18. var toString = {}.toString;
  19. var isType = function (value, type) { return toString.call(value) === '[object ' + type + ']'; };
  20. var isString = (function (str) {
  21. return isType(str, 'String');
  22. });
  23. /**
  24. * 判断是否数字
  25. * @return {Boolean} 是否数字
  26. */
  27. var isNumber = function (value) {
  28. return isType(value, 'Number');
  29. };
  30. var HTML_PREFIX = 'g-html-';
  31. var CANVAS_CAMERA_ID = 'g-canvas-camera';
  32. var HTMLRenderingPlugin = /*#__PURE__*/function () {
  33. function HTMLRenderingPlugin() {
  34. this.context = void 0;
  35. /**
  36. * wrapper for camera
  37. */
  38. this.$camera = void 0;
  39. }
  40. var _proto = HTMLRenderingPlugin.prototype;
  41. _proto.joinTransformMatrix = function joinTransformMatrix(matrix) {
  42. return "matrix(" + [matrix[0], matrix[1], matrix[4], matrix[5], matrix[12], matrix[13]].join(',') + ")";
  43. };
  44. _proto.apply = function apply(context) {
  45. var _this = this;
  46. var camera = context.camera,
  47. renderingContext = context.renderingContext,
  48. renderingService = context.renderingService;
  49. this.context = context;
  50. var canvas = renderingContext.root.ownerDocument.defaultView;
  51. var setTransform = function setTransform(object, $el) {
  52. $el.style.transform = _this.joinTransformMatrix(object.getWorldTransform());
  53. };
  54. var handleMounted = function handleMounted(e) {
  55. var object = e.target;
  56. if (object.nodeName === gLite.Shape.HTML) {
  57. if (!_this.$camera) {
  58. _this.$camera = _this.createCamera(camera);
  59. }
  60. // create DOM element
  61. var $el = _this.getOrCreateEl(object);
  62. _this.$camera.appendChild($el);
  63. // apply documentElement's style
  64. var attributes = object.ownerDocument.documentElement.attributes;
  65. Object.keys(attributes).forEach(function (name) {
  66. $el.style[name] = attributes[name];
  67. });
  68. Object.keys(object.attributes).forEach(function (name) {
  69. _this.updateAttribute(name, object);
  70. });
  71. setTransform(object, $el);
  72. gLite.runtime.nativeHTMLMap.set($el, object);
  73. }
  74. };
  75. var handleUnmounted = function handleUnmounted(e) {
  76. var object = e.target;
  77. if (object.nodeName === gLite.Shape.HTML && _this.$camera) {
  78. var $el = _this.getOrCreateEl(object);
  79. if ($el) {
  80. $el.remove();
  81. gLite.runtime.nativeHTMLMap.delete($el);
  82. }
  83. // const existedId = this.getId(object);
  84. // const $existedElement: HTMLElement | null = this.$camera.querySelector('#' + existedId);
  85. // if ($existedElement) {
  86. // this.$camera.removeChild($existedElement);
  87. // }
  88. }
  89. };
  90. var handleAttributeChanged = function handleAttributeChanged(e) {
  91. var object = e.target;
  92. if (object.nodeName === gLite.Shape.HTML) {
  93. var attrName = e.attrName;
  94. _this.updateAttribute(attrName, object);
  95. }
  96. };
  97. var handleBoundsChanged = function handleBoundsChanged(e) {
  98. var object = e.target;
  99. if (object.nodeName === gLite.Shape.HTML) {
  100. var $el = _this.getOrCreateEl(object);
  101. setTransform(object, $el);
  102. }
  103. };
  104. var handleCanvasResize = function handleCanvasResize() {
  105. if (_this.$camera) {
  106. var _this$context$config = _this.context.config,
  107. width = _this$context$config.width,
  108. height = _this$context$config.height;
  109. _this.$camera.style.width = (width || 0) + "px";
  110. _this.$camera.style.height = (height || 0) + "px";
  111. }
  112. };
  113. renderingService.hooks.init.tap(HTMLRenderingPlugin.tag, function () {
  114. canvas.addEventListener(gLite.CanvasEvent.RESIZE, handleCanvasResize);
  115. canvas.addEventListener(gLite.ElementEvent.MOUNTED, handleMounted);
  116. canvas.addEventListener(gLite.ElementEvent.UNMOUNTED, handleUnmounted);
  117. canvas.addEventListener(gLite.ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
  118. canvas.addEventListener(gLite.ElementEvent.BOUNDS_CHANGED, handleBoundsChanged);
  119. });
  120. renderingService.hooks.endFrame.tap(HTMLRenderingPlugin.tag, function () {
  121. if (_this.$camera && renderingContext.renderReasons.has(gLite.RenderReason.CAMERA_CHANGED)) {
  122. _this.$camera.style.transform = _this.joinTransformMatrix(camera.getOrthoMatrix());
  123. }
  124. });
  125. renderingService.hooks.destroy.tap(HTMLRenderingPlugin.tag, function () {
  126. // remove camera
  127. if (_this.$camera) {
  128. _this.$camera.remove();
  129. }
  130. canvas.removeEventListener(gLite.CanvasEvent.RESIZE, handleCanvasResize);
  131. canvas.removeEventListener(gLite.ElementEvent.MOUNTED, handleMounted);
  132. canvas.removeEventListener(gLite.ElementEvent.UNMOUNTED, handleUnmounted);
  133. canvas.removeEventListener(gLite.ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
  134. canvas.removeEventListener(gLite.ElementEvent.BOUNDS_CHANGED, handleBoundsChanged);
  135. });
  136. };
  137. _proto.getId = function getId(object) {
  138. return object.id || HTML_PREFIX + object.entity;
  139. };
  140. _proto.createCamera = function createCamera(camera) {
  141. var _this$context$config2 = this.context.config,
  142. doc = _this$context$config2.document,
  143. width = _this$context$config2.width,
  144. height = _this$context$config2.height;
  145. var $canvas = this.context.contextService.getDomElement();
  146. var $container = $canvas.parentNode;
  147. if ($container) {
  148. var cameraId = CANVAS_CAMERA_ID;
  149. var $existedCamera = $container.querySelector('#' + cameraId);
  150. if (!$existedCamera) {
  151. var $camera = (doc || document).createElement('div');
  152. $existedCamera = $camera;
  153. $camera.id = cameraId;
  154. // use absolute position
  155. $camera.style.position = 'absolute';
  156. // account for DOM element's offset @see https://github.com/antvis/G/issues/1150
  157. $camera.style.left = ($canvas.offsetLeft || 0) + "px";
  158. $camera.style.top = ($canvas.offsetTop || 0) + "px";
  159. $camera.style.transformOrigin = 'left top';
  160. $camera.style.transform = this.joinTransformMatrix(camera.getOrthoMatrix());
  161. // HTML elements should not overflow with canvas @see https://github.com/antvis/G/issues/1163
  162. $camera.style.overflow = 'hidden';
  163. $camera.style.pointerEvents = 'none';
  164. $camera.style.width = (width || 0) + "px";
  165. $camera.style.height = (height || 0) + "px";
  166. $container.appendChild($camera);
  167. }
  168. return $existedCamera;
  169. }
  170. return null;
  171. };
  172. _proto.getOrCreateEl = function getOrCreateEl(object) {
  173. var doc = this.context.config.document;
  174. var existedId = this.getId(object);
  175. var $existedElement = this.$camera.querySelector('#' + existedId);
  176. if (!$existedElement) {
  177. $existedElement = (doc || document).createElement('div');
  178. object.parsedStyle.$el = $existedElement;
  179. $existedElement.id = existedId;
  180. if (object.name) {
  181. $existedElement.setAttribute('name', object.name);
  182. }
  183. if (object.className) {
  184. $existedElement.className = object.className;
  185. }
  186. // use absolute position
  187. $existedElement.style.position = 'absolute';
  188. // @see https://github.com/antvis/G/issues/1150
  189. $existedElement.style.left = "0px";
  190. $existedElement.style.top = "0px";
  191. $existedElement.style['will-change'] = 'transform';
  192. $existedElement.style.transform = this.joinTransformMatrix(object.getWorldTransform());
  193. }
  194. return $existedElement;
  195. };
  196. _proto.updateAttribute = function updateAttribute(name, object) {
  197. var $el = this.getOrCreateEl(object);
  198. switch (name) {
  199. case 'innerHTML':
  200. var innerHTML = object.parsedStyle.innerHTML;
  201. if (isString(innerHTML)) {
  202. $el.innerHTML = innerHTML;
  203. } else {
  204. $el.innerHTML = '';
  205. $el.appendChild(innerHTML);
  206. }
  207. break;
  208. case 'transformOrigin':
  209. var transformOrigin = object.parsedStyle.transformOrigin;
  210. $el.style['transform-origin'] = transformOrigin[0].value + " " + transformOrigin[1].value;
  211. break;
  212. case 'width':
  213. if (gLite.runtime.enableCSSParsing) {
  214. var width = object.computedStyleMap().get('width');
  215. $el.style.width = width.toString();
  216. } else {
  217. var _width = object.parsedStyle.width;
  218. $el.style.width = isNumber(_width) ? _width + "px" : _width.toString();
  219. }
  220. break;
  221. case 'height':
  222. if (gLite.runtime.enableCSSParsing) {
  223. var height = object.computedStyleMap().get('height');
  224. $el.style.height = height.toString();
  225. } else {
  226. var _height = object.parsedStyle.height;
  227. $el.style.height = isNumber(_height) ? _height + "px" : _height.toString();
  228. }
  229. break;
  230. case 'zIndex':
  231. var zIndex = object.parsedStyle.zIndex;
  232. $el.style['z-index'] = "" + zIndex;
  233. break;
  234. case 'visibility':
  235. var visibility = object.parsedStyle.visibility;
  236. $el.style.visibility = visibility;
  237. break;
  238. case 'pointerEvents':
  239. var pointerEvents = object.parsedStyle.pointerEvents;
  240. $el.style.pointerEvents = pointerEvents;
  241. break;
  242. case 'opacity':
  243. var opacity = object.parsedStyle.opacity;
  244. $el.style.opacity = "" + opacity;
  245. break;
  246. case 'fill':
  247. var fill = object.parsedStyle.fill;
  248. var color = '';
  249. if (gLite.isCSSRGB(fill)) {
  250. if (fill.isNone) {
  251. color = 'transparent';
  252. } else {
  253. color = object.getAttribute('fill');
  254. }
  255. } else if (Array.isArray(fill)) {
  256. color = object.getAttribute('fill');
  257. } else if (gLite.isPattern(fill)) ;
  258. $el.style.background = color;
  259. break;
  260. case 'stroke':
  261. var stroke = object.parsedStyle.stroke;
  262. var borderColor = '';
  263. if (gLite.isCSSRGB(stroke)) {
  264. if (stroke.isNone) {
  265. borderColor = 'transparent';
  266. } else {
  267. borderColor = object.getAttribute('stroke');
  268. }
  269. } else if (Array.isArray(stroke)) {
  270. borderColor = object.getAttribute('stroke');
  271. } else if (gLite.isPattern(stroke)) ;
  272. $el.style['border-color'] = borderColor;
  273. $el.style['border-style'] = 'solid';
  274. break;
  275. case 'lineWidth':
  276. var lineWidth = object.parsedStyle.lineWidth;
  277. $el.style['border-width'] = (lineWidth || 0) + "px";
  278. break;
  279. case 'lineDash':
  280. $el.style['border-style'] = 'dashed';
  281. break;
  282. case 'filter':
  283. var filter = object.style.filter;
  284. $el.style.filter = filter;
  285. break;
  286. }
  287. };
  288. return HTMLRenderingPlugin;
  289. }();
  290. HTMLRenderingPlugin.tag = 'HTMLRendering';
  291. var Plugin = /*#__PURE__*/function (_AbstractRendererPlug) {
  292. _inheritsLoose(Plugin, _AbstractRendererPlug);
  293. function Plugin() {
  294. var _this;
  295. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  296. args[_key] = arguments[_key];
  297. }
  298. _this = _AbstractRendererPlug.call.apply(_AbstractRendererPlug, [this].concat(args)) || this;
  299. _this.name = 'html-renderer';
  300. return _this;
  301. }
  302. var _proto = Plugin.prototype;
  303. _proto.init = function init() {
  304. this.addRenderingPlugin(new HTMLRenderingPlugin());
  305. };
  306. _proto.destroy = function destroy() {
  307. this.removeAllRenderingPlugins();
  308. };
  309. return Plugin;
  310. }(gLite.AbstractRendererPlugin);
  311. exports.Plugin = Plugin;
  312. Object.defineProperty(exports, '__esModule', { value: true });
  313. })));