@logicflow/extension 2.2.0-alpha.0 → 2.2.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,29 +1,4 @@
1
- var __read = (this && this.__read) || function (o, n) {
2
- var m = typeof Symbol === "function" && o[Symbol.iterator];
3
- if (!m) return o;
4
- var i = m.call(o), r, ar = [], e;
5
- try {
6
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
- }
8
- catch (error) { e = { error: error }; }
9
- finally {
10
- try {
11
- if (r && !r.done && (m = i["return"])) m.call(i);
12
- }
13
- finally { if (e) throw e.error; }
14
- }
15
- return ar;
16
- };
17
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
- if (ar || !(i in from)) {
20
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
- ar[i] = from[i];
22
- }
23
- }
24
- return to.concat(ar || Array.prototype.slice.call(from));
25
- };
26
- import { EventType, formatAnchorConnectValidateData, } from '@logicflow/core';
1
+ import { EventType, formatAnchorConnectValidateData, getClosestAnchor, } from '@logicflow/core';
27
2
  import { cloneDeep } from 'lodash-es';
28
3
  import { isNodeInSegment } from './edge';
29
4
  var InsertNodeInPolyline = /** @class */ (function () {
@@ -113,28 +88,29 @@ var InsertNodeInPolyline = /** @class */ (function () {
113
88
  var _b = edges[i], sourceNodeId = _b.sourceNodeId, targetNodeId = _b.targetNodeId, id = _b.id, type = _b.type, pointsList = _b.pointsList, sourceAnchorId = _b.sourceAnchorId, targetAnchorId = _b.targetAnchorId;
114
89
  // fix https://github.com/didi/LogicFlow/issues/996
115
90
  var startPoint = cloneDeep(pointsList[0]);
116
- var endPoint = cloneDeep(crossPoints.startCrossPoint);
117
91
  this._lf.deleteEdge(id);
118
92
  var checkResult = this.checkRuleBeforeInsetNode(sourceNodeId, targetNodeId, sourceAnchorId, targetAnchorId, nodeData);
93
+ // 基于插入节点的进入交点计算出最近的“进入锚点”,用于重连原边的前半段
94
+ var entryAnchorInfo = getClosestAnchor(crossPoints.startCrossPoint, nodeModel);
95
+ var entryAnchor = entryAnchorInfo.anchor;
96
+ // 构造第一条边:原 source → 插入节点(终点为进入锚点)
119
97
  this._lf.addEdge({
120
98
  type: type,
121
99
  sourceNodeId: sourceNodeId,
122
100
  targetNodeId: nodeData.id,
123
101
  startPoint: startPoint,
124
- endPoint: endPoint,
125
- pointsList: __spreadArray(__spreadArray([], __read(pointsList.slice(0, crossIndex)), false), [
126
- crossPoints.startCrossPoint,
127
- ], false),
102
+ endPoint: entryAnchor,
128
103
  });
104
+ // 基于插入节点的离开交点计算出最近的“离开锚点”,用于重连原边的后半段
105
+ var exitAnchorInfo = getClosestAnchor(crossPoints.endCrossPoint, nodeModel);
106
+ var exitAnchor = exitAnchorInfo.anchor;
107
+ // 构造第二条边:插入节点 → 原 target(起点为离开锚点)
129
108
  this._lf.addEdge({
130
109
  type: type,
131
110
  sourceNodeId: nodeData.id,
132
111
  targetNodeId: targetNodeId,
133
- startPoint: cloneDeep(crossPoints.endCrossPoint),
112
+ startPoint: cloneDeep(exitAnchor),
134
113
  endPoint: cloneDeep(pointsList[pointsList.length - 1]),
135
- pointsList: __spreadArray([
136
- crossPoints.endCrossPoint
137
- ], __read(pointsList.slice(crossIndex)), false),
138
114
  });
139
115
  if (!checkResult.isPass) {
140
116
  this._lf.graphModel.eventCenter.emit(EventType.CONNECTION_NOT_ALLOWED, {
@@ -49,7 +49,7 @@ import { Component, observer } from '@logicflow/core';
49
49
  import { forEach, merge } from 'lodash-es';
50
50
  import Label from './Label';
51
51
  import LabelModel from './LabelModel';
52
- import { MediumEditor, defaultOptions, ColorPickerButton } from './mediumEditor';
52
+ import { MediumEditor, defaultOptions, createColorPickerButtonClass, } from './mediumEditor';
53
53
  // const { createUuid } = LogicFlowUtil
54
54
  // DONE: 解决问题,如果 LabelOverlay 设置为 Observer,拖拽 Label 时会导致 LabelOverlay 组件重新渲染,不知道为何
55
55
  // 目前解决了。流程是 拖动 -> 更新 label 的数据信息到 element model -> 重新渲染 LabelOverlay
@@ -74,7 +74,7 @@ var LabelOverlay = /** @class */ (function (_super) {
74
74
  this.editor = new MediumEditor('.lf-label-editor', merge(defaultOptions, {
75
75
  autoLink: true,
76
76
  extensions: {
77
- colorPicker: new ColorPickerButton(),
77
+ colorPicker: new (createColorPickerButtonClass(MediumEditor))(),
78
78
  },
79
79
  }));
80
80
  // TODO: 2. 在此处监听一些事件,当 node、edge 数据变化时,主动触发重新渲染,保证同步更新
@@ -99,7 +99,7 @@ var LabelOverlay = /** @class */ (function (_super) {
99
99
  this.editor = new MediumEditor('.lf-label-editor', merge(defaultOptions, {
100
100
  autoLink: true,
101
101
  extensions: {
102
- colorPicker: new ColorPickerButton(),
102
+ colorPicker: new (createColorPickerButtonClass(MediumEditor))(),
103
103
  },
104
104
  }));
105
105
  }
@@ -13,5 +13,5 @@ export declare const defaultOptions: {
13
13
  };
14
14
  disableEditing: boolean;
15
15
  };
16
- export declare const ColorPickerButton: any;
16
+ export declare function createColorPickerButtonClass(MediumEditor?: any): any;
17
17
  export { MediumEditor };
@@ -36,56 +36,93 @@ export var defaultOptions = {
36
36
  },
37
37
  disableEditing: true,
38
38
  };
39
- export var ColorPickerButton = MediumEditor.extensions.button.extend({
40
- name: 'colorpicker',
41
- tagNames: ['mark'],
42
- contentDefault: '<b>Color</b>',
43
- aria: 'Color Picker',
44
- action: 'colorPicker',
45
- init: function () {
46
- var _this = this;
47
- rangy.init();
48
- MediumEditor.extensions.button.prototype.init.call(this);
49
- this.colorPicker = new Picker({
50
- parent: this.button,
51
- color: '#000',
52
- onDone: function (res) {
53
- if (_this.coloredText && _this.coloredText.isAppliedToSelection()) {
54
- _this.coloredText.undoToSelection();
55
- }
56
- _this.coloredText = rangy.createClassApplier('colored', {
57
- elementTagName: 'span',
58
- elementProperties: {
59
- style: {
60
- color: res.hex,
61
- },
62
- },
63
- normalize: true,
64
- });
65
- _this.coloredText.toggleSelection();
66
- _this.base.checkContentChanged();
67
- _this.setInactive();
68
- },
69
- });
70
- },
71
- getButton: function () {
72
- return this.button;
73
- },
74
- handleClick: function () {
75
- this.setActive();
76
- this.colorPicker.show();
77
- },
78
- isAlreadyApplied: function (node) {
79
- return node.nodeName.toLowerCase() === 'mark';
80
- },
81
- isActive: function () {
82
- return this.button.classList.contains('medium-editor-button-active');
83
- },
84
- setInactive: function () {
85
- this.button.classList.remove('medium-editor-button-active');
86
- },
87
- setActive: function () {
88
- this.button.classList.add('medium-editor-button-active');
89
- },
90
- });
39
+ export function createColorPickerButtonClass(MediumEditor) {
40
+ var _a, _b;
41
+ var ButtonBase = ((_a = MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.extensions) === null || _a === void 0 ? void 0 : _a.button) || ((_b = MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.extensions) === null || _b === void 0 ? void 0 : _b.button);
42
+ var ExtensionBase = (MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.Extension) || (MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.Extension);
43
+ // Button 扩展基类不可用时,回退到 Extension 基类,避免在模块加载阶段抛错
44
+ var Base = ButtonBase || ExtensionBase;
45
+ if (!Base) {
46
+ console.warn('MediumEditor button/extension base not available; using noop extension');
47
+ return /** @class */ (function () {
48
+ function class_1() {
49
+ }
50
+ return class_1;
51
+ }());
52
+ }
53
+ return Base.extend({
54
+ name: 'colorpicker',
55
+ tagNames: ['mark'],
56
+ contentDefault: '<b>Color</b>',
57
+ aria: 'Color Picker',
58
+ action: 'colorPicker',
59
+ init: function () {
60
+ var _this = this;
61
+ var _a, _b;
62
+ try {
63
+ rangy.init();
64
+ }
65
+ catch (_c) {
66
+ console.error('rangy.init failed');
67
+ }
68
+ // 初始化按钮(ButtonBase 才有 prototype.init)
69
+ try {
70
+ ;
71
+ (_b = (_a = ButtonBase === null || ButtonBase === void 0 ? void 0 : ButtonBase.prototype) === null || _a === void 0 ? void 0 : _a.init) === null || _b === void 0 ? void 0 : _b.call(this);
72
+ }
73
+ catch (_d) {
74
+ console.error('ButtonBase.init failed');
75
+ }
76
+ this.colorPicker = new Picker({
77
+ parent: this.button || undefined,
78
+ color: '#000',
79
+ onDone: function (res) {
80
+ var _a, _b, _c, _d, _e;
81
+ try {
82
+ if (_this.coloredText && ((_b = (_a = _this.coloredText).isAppliedToSelection) === null || _b === void 0 ? void 0 : _b.call(_a))) {
83
+ _this.coloredText.undoToSelection();
84
+ }
85
+ _this.coloredText = rangy.createClassApplier('colored', {
86
+ elementTagName: 'span',
87
+ elementProperties: { style: { color: res.hex } },
88
+ normalize: true,
89
+ });
90
+ _this.coloredText.toggleSelection();
91
+ (_d = (_c = _this.base) === null || _c === void 0 ? void 0 : _c.checkContentChanged) === null || _d === void 0 ? void 0 : _d.call(_c);
92
+ (_e = _this.setInactive) === null || _e === void 0 ? void 0 : _e.call(_this);
93
+ }
94
+ catch (_f) {
95
+ console.error('Picker.onDone failed');
96
+ }
97
+ },
98
+ });
99
+ },
100
+ getButton: function () {
101
+ return this.button;
102
+ },
103
+ handleClick: function () {
104
+ var _a, _b, _c;
105
+ (_a = this.setActive) === null || _a === void 0 ? void 0 : _a.call(this);
106
+ (_c = (_b = this.colorPicker) === null || _b === void 0 ? void 0 : _b.show) === null || _c === void 0 ? void 0 : _c.call(_b);
107
+ },
108
+ isAlreadyApplied: function (node) {
109
+ var _a, _b;
110
+ return ((_b = (_a = node === null || node === void 0 ? void 0 : node.nodeName) === null || _a === void 0 ? void 0 : _a.toLowerCase) === null || _b === void 0 ? void 0 : _b.call(_a)) === 'mark';
111
+ },
112
+ isActive: function () {
113
+ var _a, _b;
114
+ return (_b = (_a = this.button) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.contains('medium-editor-button-active');
115
+ },
116
+ setInactive: function () {
117
+ var _a, _b;
118
+ ;
119
+ (_b = (_a = this.button) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.remove('medium-editor-button-active');
120
+ },
121
+ setActive: function () {
122
+ var _a, _b;
123
+ ;
124
+ (_b = (_a = this.button) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.add('medium-editor-button-active');
125
+ },
126
+ });
127
+ }
91
128
  export { MediumEditor };
@@ -666,7 +666,7 @@ var Snapshot = /** @class */ (function () {
666
666
  // 内部方法处理blob转换
667
667
  Snapshot.prototype.snapshotBlob = function (toImageOptions, baseFileType, backgroundColor) {
668
668
  return __awaiter(this, void 0, void 0, function () {
669
- var _a, fileType, svg;
669
+ var _a, fileType, svg, copy, svgString, blob;
670
670
  var _this = this;
671
671
  return __generator(this, function (_b) {
672
672
  switch (_b.label) {
@@ -676,18 +676,31 @@ var Snapshot = /** @class */ (function () {
676
676
  return [4 /*yield*/, updateImageSource(svg)];
677
677
  case 1:
678
678
  _b.sent();
679
- return [2 /*return*/, new Promise(function (resolve) {
680
- _this.getCanvasData(svg, __assign({ backgroundColor: backgroundColor }, (toImageOptions !== null && toImageOptions !== void 0 ? toImageOptions : {}))).then(function (canvas) {
681
- canvas.toBlob(function (blob) {
682
- // 输出图片数据以及图片宽高
683
- resolve({
684
- data: blob,
685
- width: canvas.width,
686
- height: canvas.height,
687
- });
688
- }, "image/".concat(fileType !== null && fileType !== void 0 ? fileType : 'png'));
689
- });
690
- })];
679
+ if (!(fileType === 'svg')) return [3 /*break*/, 3];
680
+ return [4 /*yield*/, this.cloneSvg(svg)];
681
+ case 2:
682
+ copy = _b.sent();
683
+ svgString = new XMLSerializer().serializeToString(copy);
684
+ blob = new Blob([svgString], {
685
+ type: 'image/svg+xml;charset=utf-8',
686
+ });
687
+ return [2 /*return*/, {
688
+ data: blob,
689
+ width: 0,
690
+ height: 0,
691
+ }];
692
+ case 3: return [2 /*return*/, new Promise(function (resolve) {
693
+ _this.getCanvasData(svg, __assign({ backgroundColor: backgroundColor }, (toImageOptions !== null && toImageOptions !== void 0 ? toImageOptions : {}))).then(function (canvas) {
694
+ canvas.toBlob(function (blob) {
695
+ // 输出图片数据以及图片宽高
696
+ resolve({
697
+ data: blob,
698
+ width: canvas.width,
699
+ height: canvas.height,
700
+ });
701
+ }, "image/".concat(fileType !== null && fileType !== void 0 ? fileType : 'png'));
702
+ });
703
+ })];
691
704
  }
692
705
  });
693
706
  });
@@ -1,29 +1,4 @@
1
1
  "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
- }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
- if (ar || !(i in from)) {
21
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
- ar[i] = from[i];
23
- }
24
- }
25
- return to.concat(ar || Array.prototype.slice.call(from));
26
- };
27
2
  Object.defineProperty(exports, "__esModule", { value: true });
28
3
  exports.InsertNodeInPolyline = void 0;
29
4
  var core_1 = require("@logicflow/core");
@@ -116,28 +91,29 @@ var InsertNodeInPolyline = /** @class */ (function () {
116
91
  var _b = edges[i], sourceNodeId = _b.sourceNodeId, targetNodeId = _b.targetNodeId, id = _b.id, type = _b.type, pointsList = _b.pointsList, sourceAnchorId = _b.sourceAnchorId, targetAnchorId = _b.targetAnchorId;
117
92
  // fix https://github.com/didi/LogicFlow/issues/996
118
93
  var startPoint = (0, lodash_es_1.cloneDeep)(pointsList[0]);
119
- var endPoint = (0, lodash_es_1.cloneDeep)(crossPoints.startCrossPoint);
120
94
  this._lf.deleteEdge(id);
121
95
  var checkResult = this.checkRuleBeforeInsetNode(sourceNodeId, targetNodeId, sourceAnchorId, targetAnchorId, nodeData);
96
+ // 基于插入节点的进入交点计算出最近的“进入锚点”,用于重连原边的前半段
97
+ var entryAnchorInfo = (0, core_1.getClosestAnchor)(crossPoints.startCrossPoint, nodeModel);
98
+ var entryAnchor = entryAnchorInfo.anchor;
99
+ // 构造第一条边:原 source → 插入节点(终点为进入锚点)
122
100
  this._lf.addEdge({
123
101
  type: type,
124
102
  sourceNodeId: sourceNodeId,
125
103
  targetNodeId: nodeData.id,
126
104
  startPoint: startPoint,
127
- endPoint: endPoint,
128
- pointsList: __spreadArray(__spreadArray([], __read(pointsList.slice(0, crossIndex)), false), [
129
- crossPoints.startCrossPoint,
130
- ], false),
105
+ endPoint: entryAnchor,
131
106
  });
107
+ // 基于插入节点的离开交点计算出最近的“离开锚点”,用于重连原边的后半段
108
+ var exitAnchorInfo = (0, core_1.getClosestAnchor)(crossPoints.endCrossPoint, nodeModel);
109
+ var exitAnchor = exitAnchorInfo.anchor;
110
+ // 构造第二条边:插入节点 → 原 target(起点为离开锚点)
132
111
  this._lf.addEdge({
133
112
  type: type,
134
113
  sourceNodeId: nodeData.id,
135
114
  targetNodeId: targetNodeId,
136
- startPoint: (0, lodash_es_1.cloneDeep)(crossPoints.endCrossPoint),
115
+ startPoint: (0, lodash_es_1.cloneDeep)(exitAnchor),
137
116
  endPoint: (0, lodash_es_1.cloneDeep)(pointsList[pointsList.length - 1]),
138
- pointsList: __spreadArray([
139
- crossPoints.endCrossPoint
140
- ], __read(pointsList.slice(crossIndex)), false),
141
117
  });
142
118
  if (!checkResult.isPass) {
143
119
  this._lf.graphModel.eventCenter.emit(core_1.EventType.CONNECTION_NOT_ALLOWED, {
@@ -80,7 +80,7 @@ var LabelOverlay = /** @class */ (function (_super) {
80
80
  this.editor = new mediumEditor_1.MediumEditor('.lf-label-editor', (0, lodash_es_1.merge)(mediumEditor_1.defaultOptions, {
81
81
  autoLink: true,
82
82
  extensions: {
83
- colorPicker: new mediumEditor_1.ColorPickerButton(),
83
+ colorPicker: new ((0, mediumEditor_1.createColorPickerButtonClass)(mediumEditor_1.MediumEditor))(),
84
84
  },
85
85
  }));
86
86
  // TODO: 2. 在此处监听一些事件,当 node、edge 数据变化时,主动触发重新渲染,保证同步更新
@@ -105,7 +105,7 @@ var LabelOverlay = /** @class */ (function (_super) {
105
105
  this.editor = new mediumEditor_1.MediumEditor('.lf-label-editor', (0, lodash_es_1.merge)(mediumEditor_1.defaultOptions, {
106
106
  autoLink: true,
107
107
  extensions: {
108
- colorPicker: new mediumEditor_1.ColorPickerButton(),
108
+ colorPicker: new ((0, mediumEditor_1.createColorPickerButtonClass)(mediumEditor_1.MediumEditor))(),
109
109
  },
110
110
  }));
111
111
  }
@@ -13,5 +13,5 @@ export declare const defaultOptions: {
13
13
  };
14
14
  disableEditing: boolean;
15
15
  };
16
- export declare const ColorPickerButton: any;
16
+ export declare function createColorPickerButtonClass(MediumEditor?: any): any;
17
17
  export { MediumEditor };
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.MediumEditor = exports.ColorPickerButton = exports.defaultOptions = void 0;
6
+ exports.MediumEditor = exports.createColorPickerButtonClass = exports.defaultOptions = void 0;
7
7
  var medium_editor_1 = __importDefault(require("medium-editor"));
8
8
  exports.MediumEditor = medium_editor_1.default;
9
9
  var vanilla_picker_1 = __importDefault(require("vanilla-picker"));
@@ -43,55 +43,93 @@ exports.defaultOptions = {
43
43
  },
44
44
  disableEditing: true,
45
45
  };
46
- exports.ColorPickerButton = medium_editor_1.default.extensions.button.extend({
47
- name: 'colorpicker',
48
- tagNames: ['mark'],
49
- contentDefault: '<b>Color</b>',
50
- aria: 'Color Picker',
51
- action: 'colorPicker',
52
- init: function () {
53
- var _this = this;
54
- rangy_1.default.init();
55
- medium_editor_1.default.extensions.button.prototype.init.call(this);
56
- this.colorPicker = new vanilla_picker_1.default({
57
- parent: this.button,
58
- color: '#000',
59
- onDone: function (res) {
60
- if (_this.coloredText && _this.coloredText.isAppliedToSelection()) {
61
- _this.coloredText.undoToSelection();
62
- }
63
- _this.coloredText = rangy_1.default.createClassApplier('colored', {
64
- elementTagName: 'span',
65
- elementProperties: {
66
- style: {
67
- color: res.hex,
68
- },
69
- },
70
- normalize: true,
71
- });
72
- _this.coloredText.toggleSelection();
73
- _this.base.checkContentChanged();
74
- _this.setInactive();
75
- },
76
- });
77
- },
78
- getButton: function () {
79
- return this.button;
80
- },
81
- handleClick: function () {
82
- this.setActive();
83
- this.colorPicker.show();
84
- },
85
- isAlreadyApplied: function (node) {
86
- return node.nodeName.toLowerCase() === 'mark';
87
- },
88
- isActive: function () {
89
- return this.button.classList.contains('medium-editor-button-active');
90
- },
91
- setInactive: function () {
92
- this.button.classList.remove('medium-editor-button-active');
93
- },
94
- setActive: function () {
95
- this.button.classList.add('medium-editor-button-active');
96
- },
97
- });
46
+ function createColorPickerButtonClass(MediumEditor) {
47
+ var _a, _b;
48
+ var ButtonBase = ((_a = MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.extensions) === null || _a === void 0 ? void 0 : _a.button) || ((_b = MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.extensions) === null || _b === void 0 ? void 0 : _b.button);
49
+ var ExtensionBase = (MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.Extension) || (MediumEditor === null || MediumEditor === void 0 ? void 0 : MediumEditor.Extension);
50
+ // Button 扩展基类不可用时,回退到 Extension 基类,避免在模块加载阶段抛错
51
+ var Base = ButtonBase || ExtensionBase;
52
+ if (!Base) {
53
+ console.warn('MediumEditor button/extension base not available; using noop extension');
54
+ return /** @class */ (function () {
55
+ function class_1() {
56
+ }
57
+ return class_1;
58
+ }());
59
+ }
60
+ return Base.extend({
61
+ name: 'colorpicker',
62
+ tagNames: ['mark'],
63
+ contentDefault: '<b>Color</b>',
64
+ aria: 'Color Picker',
65
+ action: 'colorPicker',
66
+ init: function () {
67
+ var _this = this;
68
+ var _a, _b;
69
+ try {
70
+ rangy_1.default.init();
71
+ }
72
+ catch (_c) {
73
+ console.error('rangy.init failed');
74
+ }
75
+ // 初始化按钮(ButtonBase 才有 prototype.init)
76
+ try {
77
+ ;
78
+ (_b = (_a = ButtonBase === null || ButtonBase === void 0 ? void 0 : ButtonBase.prototype) === null || _a === void 0 ? void 0 : _a.init) === null || _b === void 0 ? void 0 : _b.call(this);
79
+ }
80
+ catch (_d) {
81
+ console.error('ButtonBase.init failed');
82
+ }
83
+ this.colorPicker = new vanilla_picker_1.default({
84
+ parent: this.button || undefined,
85
+ color: '#000',
86
+ onDone: function (res) {
87
+ var _a, _b, _c, _d, _e;
88
+ try {
89
+ if (_this.coloredText && ((_b = (_a = _this.coloredText).isAppliedToSelection) === null || _b === void 0 ? void 0 : _b.call(_a))) {
90
+ _this.coloredText.undoToSelection();
91
+ }
92
+ _this.coloredText = rangy_1.default.createClassApplier('colored', {
93
+ elementTagName: 'span',
94
+ elementProperties: { style: { color: res.hex } },
95
+ normalize: true,
96
+ });
97
+ _this.coloredText.toggleSelection();
98
+ (_d = (_c = _this.base) === null || _c === void 0 ? void 0 : _c.checkContentChanged) === null || _d === void 0 ? void 0 : _d.call(_c);
99
+ (_e = _this.setInactive) === null || _e === void 0 ? void 0 : _e.call(_this);
100
+ }
101
+ catch (_f) {
102
+ console.error('Picker.onDone failed');
103
+ }
104
+ },
105
+ });
106
+ },
107
+ getButton: function () {
108
+ return this.button;
109
+ },
110
+ handleClick: function () {
111
+ var _a, _b, _c;
112
+ (_a = this.setActive) === null || _a === void 0 ? void 0 : _a.call(this);
113
+ (_c = (_b = this.colorPicker) === null || _b === void 0 ? void 0 : _b.show) === null || _c === void 0 ? void 0 : _c.call(_b);
114
+ },
115
+ isAlreadyApplied: function (node) {
116
+ var _a, _b;
117
+ return ((_b = (_a = node === null || node === void 0 ? void 0 : node.nodeName) === null || _a === void 0 ? void 0 : _a.toLowerCase) === null || _b === void 0 ? void 0 : _b.call(_a)) === 'mark';
118
+ },
119
+ isActive: function () {
120
+ var _a, _b;
121
+ return (_b = (_a = this.button) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.contains('medium-editor-button-active');
122
+ },
123
+ setInactive: function () {
124
+ var _a, _b;
125
+ ;
126
+ (_b = (_a = this.button) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.remove('medium-editor-button-active');
127
+ },
128
+ setActive: function () {
129
+ var _a, _b;
130
+ ;
131
+ (_b = (_a = this.button) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.add('medium-editor-button-active');
132
+ },
133
+ });
134
+ }
135
+ exports.createColorPickerButtonClass = createColorPickerButtonClass;
@@ -669,7 +669,7 @@ var Snapshot = /** @class */ (function () {
669
669
  // 内部方法处理blob转换
670
670
  Snapshot.prototype.snapshotBlob = function (toImageOptions, baseFileType, backgroundColor) {
671
671
  return __awaiter(this, void 0, void 0, function () {
672
- var _a, fileType, svg;
672
+ var _a, fileType, svg, copy, svgString, blob;
673
673
  var _this = this;
674
674
  return __generator(this, function (_b) {
675
675
  switch (_b.label) {
@@ -679,18 +679,31 @@ var Snapshot = /** @class */ (function () {
679
679
  return [4 /*yield*/, (0, utils_1.updateImageSource)(svg)];
680
680
  case 1:
681
681
  _b.sent();
682
- return [2 /*return*/, new Promise(function (resolve) {
683
- _this.getCanvasData(svg, __assign({ backgroundColor: backgroundColor }, (toImageOptions !== null && toImageOptions !== void 0 ? toImageOptions : {}))).then(function (canvas) {
684
- canvas.toBlob(function (blob) {
685
- // 输出图片数据以及图片宽高
686
- resolve({
687
- data: blob,
688
- width: canvas.width,
689
- height: canvas.height,
690
- });
691
- }, "image/".concat(fileType !== null && fileType !== void 0 ? fileType : 'png'));
692
- });
693
- })];
682
+ if (!(fileType === 'svg')) return [3 /*break*/, 3];
683
+ return [4 /*yield*/, this.cloneSvg(svg)];
684
+ case 2:
685
+ copy = _b.sent();
686
+ svgString = new XMLSerializer().serializeToString(copy);
687
+ blob = new Blob([svgString], {
688
+ type: 'image/svg+xml;charset=utf-8',
689
+ });
690
+ return [2 /*return*/, {
691
+ data: blob,
692
+ width: 0,
693
+ height: 0,
694
+ }];
695
+ case 3: return [2 /*return*/, new Promise(function (resolve) {
696
+ _this.getCanvasData(svg, __assign({ backgroundColor: backgroundColor }, (toImageOptions !== null && toImageOptions !== void 0 ? toImageOptions : {}))).then(function (canvas) {
697
+ canvas.toBlob(function (blob) {
698
+ // 输出图片数据以及图片宽高
699
+ resolve({
700
+ data: blob,
701
+ width: canvas.width,
702
+ height: canvas.height,
703
+ });
704
+ }, "image/".concat(fileType !== null && fileType !== void 0 ? fileType : 'png'));
705
+ });
706
+ })];
694
707
  }
695
708
  });
696
709
  });