SaveAsImage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. /* global window, Uint8Array, document */
  42. import env from 'zrender/lib/core/env.js';
  43. import { ToolboxFeature } from '../featureManager.js';
  44. import tokens from '../../../visual/tokens.js';
  45. var SaveAsImage = /** @class */function (_super) {
  46. __extends(SaveAsImage, _super);
  47. function SaveAsImage() {
  48. return _super !== null && _super.apply(this, arguments) || this;
  49. }
  50. SaveAsImage.prototype.onclick = function (ecModel, api) {
  51. var model = this.model;
  52. var title = model.get('name') || ecModel.get('title.0.text') || 'echarts';
  53. var isSvg = api.getZr().painter.getType() === 'svg';
  54. var type = isSvg ? 'svg' : model.get('type', true) || 'png';
  55. var url = api.getConnectedDataURL({
  56. type: type,
  57. backgroundColor: model.get('backgroundColor', true) || ecModel.get('backgroundColor') || tokens.color.neutral00,
  58. connectedBackgroundColor: model.get('connectedBackgroundColor'),
  59. excludeComponents: model.get('excludeComponents'),
  60. pixelRatio: model.get('pixelRatio')
  61. });
  62. var browser = env.browser;
  63. // Chrome, Firefox, New Edge
  64. if (typeof MouseEvent === 'function' && (browser.newEdge || !browser.ie && !browser.edge)) {
  65. var $a = document.createElement('a');
  66. $a.download = title + '.' + type;
  67. $a.target = '_blank';
  68. $a.href = url;
  69. var evt = new MouseEvent('click', {
  70. // some micro front-end framework, window maybe is a Proxy
  71. view: document.defaultView,
  72. bubbles: true,
  73. cancelable: false
  74. });
  75. $a.dispatchEvent(evt);
  76. }
  77. // IE or old Edge
  78. else {
  79. // @ts-ignore
  80. if (window.navigator.msSaveOrOpenBlob || isSvg) {
  81. var parts = url.split(',');
  82. // data:[<mime type>][;charset=<charset>][;base64],<encoded data>
  83. var base64Encoded = parts[0].indexOf('base64') > -1;
  84. var bstr = isSvg
  85. // should decode the svg data uri first
  86. ? decodeURIComponent(parts[1]) : parts[1];
  87. // only `atob` when the data uri is encoded with base64
  88. // otherwise, like `svg` data uri exported by zrender,
  89. // there will be an error, for it's not encoded with base64.
  90. // (just a url-encoded string through `encodeURIComponent`)
  91. base64Encoded && (bstr = window.atob(bstr));
  92. var filename = title + '.' + type;
  93. // @ts-ignore
  94. if (window.navigator.msSaveOrOpenBlob) {
  95. var n = bstr.length;
  96. var u8arr = new Uint8Array(n);
  97. while (n--) {
  98. u8arr[n] = bstr.charCodeAt(n);
  99. }
  100. var blob = new Blob([u8arr]); // @ts-ignore
  101. window.navigator.msSaveOrOpenBlob(blob, filename);
  102. } else {
  103. var frame = document.createElement('iframe');
  104. document.body.appendChild(frame);
  105. var cw = frame.contentWindow;
  106. var doc = cw.document;
  107. doc.open('image/svg+xml', 'replace');
  108. doc.write(bstr);
  109. doc.close();
  110. cw.focus();
  111. doc.execCommand('SaveAs', true, filename);
  112. document.body.removeChild(frame);
  113. }
  114. } else {
  115. var lang = model.get('lang');
  116. var html = '' + '<body style="margin:0;">' + '<img src="' + url + '" style="max-width:100%;" title="' + (lang && lang[0] || '') + '" />' + '</body>';
  117. var tab = window.open();
  118. tab.document.write(html);
  119. tab.document.title = title;
  120. }
  121. }
  122. };
  123. SaveAsImage.getDefaultOption = function (ecModel) {
  124. var defaultOption = {
  125. show: true,
  126. icon: 'M4.7,22.9L29.3,45.5L54.7,23.4M4.6,43.6L4.6,58L53.8,58L53.8,43.6M29.2,45.1L29.2,0',
  127. title: ecModel.getLocaleModel().get(['toolbox', 'saveAsImage', 'title']),
  128. type: 'png',
  129. // Default use option.backgroundColor
  130. // backgroundColor: '#fff',
  131. connectedBackgroundColor: tokens.color.neutral00,
  132. name: '',
  133. excludeComponents: ['toolbox'],
  134. // use current pixel ratio of device by default
  135. // pixelRatio: 1,
  136. lang: ecModel.getLocaleModel().get(['toolbox', 'saveAsImage', 'lang'])
  137. };
  138. return defaultOption;
  139. };
  140. return SaveAsImage;
  141. }(ToolboxFeature);
  142. export default SaveAsImage;