@hprint/core 0.0.7 → 0.0.8

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.
package/src/index.ts CHANGED
@@ -1,11 +1,11 @@
1
- import Editor from './Editor';
2
- import Utils from './utils/utils';
3
- import CustomRect from './objects/CustomRect';
4
- import CustomTextbox from './objects/CustomTextbox';
5
- import { fabric } from 'fabric';
6
- import type { Canvas, Point, IEvent } from 'fabric/fabric-impl';
7
-
8
- export { Utils, CustomRect, CustomTextbox, fabric, Canvas, Point, IEvent };
9
- export default Editor;
10
-
11
- export * from './interface/Editor';
1
+ import Editor from './Editor';
2
+ import Utils from './utils/utils';
3
+ import CustomRect from './objects/CustomRect';
4
+ import CustomTextbox from './objects/CustomTextbox';
5
+ import { fabric } from 'fabric';
6
+ import type { Canvas, Point, IEvent } from 'fabric/fabric-impl';
7
+
8
+ export { Utils, CustomRect, CustomTextbox, fabric, Canvas, Point, IEvent };
9
+ export default Editor;
10
+
11
+ export * from './interface/Editor';
@@ -1,21 +1,21 @@
1
- import { fabric } from 'fabric';
2
-
3
- fabric.Rect = fabric.util.createClass(fabric.Rect, {
4
- type: 'rect',
5
- initialize: function (options) {
6
- options || (options = {});
7
- this.callSuper('initialize', options);
8
- },
9
- _render(ctx) {
10
- const roundValue = this.roundValue || 0;
11
- this.rx = (1 / this.scaleX) * roundValue;
12
- this.ry = (1 / this.scaleY) * roundValue;
13
- this.callSuper('_render', ctx);
14
- },
15
- });
16
-
17
- fabric.Rect.fromObject = function (object, callback) {
18
- return fabric.Object._fromObject('Rect', object, callback);
19
- };
20
-
21
- export default fabric.Rect;
1
+ import { fabric } from 'fabric';
2
+
3
+ fabric.Rect = fabric.util.createClass(fabric.Rect, {
4
+ type: 'rect',
5
+ initialize: function (options) {
6
+ options || (options = {});
7
+ this.callSuper('initialize', options);
8
+ },
9
+ _render(ctx) {
10
+ const roundValue = this.roundValue || 0;
11
+ this.rx = (1 / this.scaleX) * roundValue;
12
+ this.ry = (1 / this.scaleY) * roundValue;
13
+ this.callSuper('_render', ctx);
14
+ },
15
+ });
16
+
17
+ fabric.Rect.fromObject = function (object, callback) {
18
+ return fabric.Object._fromObject('Rect', object, callback);
19
+ };
20
+
21
+ export default fabric.Rect;
@@ -1,165 +1,165 @@
1
- /*
2
- * 文本框,修改两端对齐逻辑
3
- */
4
- import { fabric } from 'fabric';
5
-
6
- fabric.Textbox = fabric.util.createClass(fabric.Textbox, {
7
- type: 'textbox',
8
-
9
- _renderChars: function (method, ctx, line, left, top, lineIndex) {
10
- // set proper line offset
11
- var lineHeight = this.getHeightOfLine(lineIndex),
12
- isJustify = this.textAlign.indexOf('justify') !== -1,
13
- actualStyle,
14
- nextStyle,
15
- charsToRender = '',
16
- charBox,
17
- boxWidth = 0,
18
- timeToRender,
19
- path = this.path,
20
- shortCut =
21
- !isJustify &&
22
- this.charSpacing === 0 &&
23
- this.isEmptyStyles(lineIndex) &&
24
- !path,
25
- isLtr = this.direction === 'ltr',
26
- sign = this.direction === 'ltr' ? 1 : -1,
27
- drawingLeft,
28
- currentDirection = ctx.canvas.getAttribute('dir');
29
- ctx.save();
30
- if (currentDirection !== this.direction) {
31
- ctx.canvas.setAttribute('dir', isLtr ? 'ltr' : 'rtl');
32
- ctx.direction = isLtr ? 'ltr' : 'rtl';
33
- ctx.textAlign = isLtr ? 'left' : 'right';
34
- }
35
- top -= (lineHeight * this._fontSizeFraction) / this.lineHeight;
36
- if (shortCut) {
37
- // render all the line in one pass without checking
38
- // drawingLeft = isLtr ? left : left - this.getLineWidth(lineIndex);
39
- this._renderChar(
40
- method,
41
- ctx,
42
- lineIndex,
43
- 0,
44
- line.join(''),
45
- left,
46
- top,
47
- lineHeight
48
- );
49
- ctx.restore();
50
- return;
51
- }
52
- for (var i = 0, len = line.length - 1; i <= len; i++) {
53
- timeToRender = i === len || this.charSpacing || path;
54
- charsToRender += line[i];
55
- charBox = this.__charBounds[lineIndex][i];
56
- if (boxWidth === 0) {
57
- left += sign * (charBox.kernedWidth - charBox.width);
58
- boxWidth += charBox.width;
59
- } else {
60
- boxWidth += charBox.kernedWidth;
61
- }
62
- if (isJustify && !timeToRender) {
63
- if (this._reSpaceAndTab.test(line[i])) {
64
- timeToRender = true;
65
- }
66
- }
67
- if (!timeToRender) {
68
- // if we have charSpacing, we render char by char
69
- actualStyle =
70
- actualStyle ||
71
- this.getCompleteStyleDeclaration(lineIndex, i);
72
- nextStyle = this.getCompleteStyleDeclaration(lineIndex, i + 1);
73
- timeToRender = fabric.util.hasStyleChanged(
74
- actualStyle,
75
- nextStyle,
76
- false
77
- );
78
- }
79
- // if (timeToRender) {
80
- if (path) {
81
- ctx.save();
82
- ctx.translate(charBox.renderLeft, charBox.renderTop);
83
- ctx.rotate(charBox.angle);
84
- this._renderChar(
85
- method,
86
- ctx,
87
- lineIndex,
88
- i,
89
- charsToRender,
90
- -boxWidth / 2,
91
- 0,
92
- lineHeight
93
- );
94
- ctx.restore();
95
- } else {
96
- drawingLeft = left;
97
- this._renderChar(
98
- method,
99
- ctx,
100
- lineIndex,
101
- i,
102
- charsToRender,
103
- drawingLeft,
104
- top,
105
- lineHeight
106
- );
107
- }
108
- charsToRender = '';
109
- actualStyle = nextStyle;
110
- left += sign * boxWidth;
111
- boxWidth = 0;
112
- // }
113
- }
114
- ctx.restore();
115
- },
116
- enlargeSpaces: function () {
117
- var diffSpace,
118
- currentLineWidth,
119
- numberOfSpaces,
120
- accumulatedSpace,
121
- line,
122
- charBound,
123
- spaces;
124
- for (var i = 0, len = this._textLines.length; i < len; i++) {
125
- if (
126
- this.textAlign !== 'justify' &&
127
- (i === len - 1 || this.isEndOfWrapping(i))
128
- ) {
129
- continue;
130
- }
131
- accumulatedSpace = 0;
132
- line = this._textLines[i];
133
- currentLineWidth = this.getLineWidth(i);
134
- if (
135
- currentLineWidth < this.width &&
136
- (spaces = this.textLines[i].split('')) &&
137
- spaces.length > 1
138
- ) {
139
- numberOfSpaces = spaces.length;
140
- diffSpace =
141
- (this.width - currentLineWidth) / (numberOfSpaces - 1);
142
- for (var j = 0, jlen = line.length; j <= jlen; j++) {
143
- charBound = this.__charBounds[i][j];
144
- // if (this._reSpaceAndTab.test(line[j])) {
145
- charBound.left += accumulatedSpace;
146
- if (j < jlen - 1) {
147
- charBound.width += diffSpace;
148
- charBound.kernedWidth += diffSpace;
149
- accumulatedSpace += diffSpace;
150
- }
151
- // } else {
152
- // charBound.left += accumulatedSpace;
153
- // }
154
- }
155
- }
156
- }
157
- },
158
- });
159
-
160
- fabric.Textbox.fromObject = function (options, callback) {
161
- const { text } = options;
162
- return callback(new fabric.Textbox(text, options));
163
- };
164
-
165
- export default fabric.Rect;
1
+ /*
2
+ * 文本框,修改两端对齐逻辑
3
+ */
4
+ import { fabric } from 'fabric';
5
+
6
+ fabric.Textbox = fabric.util.createClass(fabric.Textbox, {
7
+ type: 'textbox',
8
+
9
+ _renderChars: function (method, ctx, line, left, top, lineIndex) {
10
+ // set proper line offset
11
+ var lineHeight = this.getHeightOfLine(lineIndex),
12
+ isJustify = this.textAlign.indexOf('justify') !== -1,
13
+ actualStyle,
14
+ nextStyle,
15
+ charsToRender = '',
16
+ charBox,
17
+ boxWidth = 0,
18
+ timeToRender,
19
+ path = this.path,
20
+ shortCut =
21
+ !isJustify &&
22
+ this.charSpacing === 0 &&
23
+ this.isEmptyStyles(lineIndex) &&
24
+ !path,
25
+ isLtr = this.direction === 'ltr',
26
+ sign = this.direction === 'ltr' ? 1 : -1,
27
+ drawingLeft,
28
+ currentDirection = ctx.canvas.getAttribute('dir');
29
+ ctx.save();
30
+ if (currentDirection !== this.direction) {
31
+ ctx.canvas.setAttribute('dir', isLtr ? 'ltr' : 'rtl');
32
+ ctx.direction = isLtr ? 'ltr' : 'rtl';
33
+ ctx.textAlign = isLtr ? 'left' : 'right';
34
+ }
35
+ top -= (lineHeight * this._fontSizeFraction) / this.lineHeight;
36
+ if (shortCut) {
37
+ // render all the line in one pass without checking
38
+ // drawingLeft = isLtr ? left : left - this.getLineWidth(lineIndex);
39
+ this._renderChar(
40
+ method,
41
+ ctx,
42
+ lineIndex,
43
+ 0,
44
+ line.join(''),
45
+ left,
46
+ top,
47
+ lineHeight
48
+ );
49
+ ctx.restore();
50
+ return;
51
+ }
52
+ for (var i = 0, len = line.length - 1; i <= len; i++) {
53
+ timeToRender = i === len || this.charSpacing || path;
54
+ charsToRender += line[i];
55
+ charBox = this.__charBounds[lineIndex][i];
56
+ if (boxWidth === 0) {
57
+ left += sign * (charBox.kernedWidth - charBox.width);
58
+ boxWidth += charBox.width;
59
+ } else {
60
+ boxWidth += charBox.kernedWidth;
61
+ }
62
+ if (isJustify && !timeToRender) {
63
+ if (this._reSpaceAndTab.test(line[i])) {
64
+ timeToRender = true;
65
+ }
66
+ }
67
+ if (!timeToRender) {
68
+ // if we have charSpacing, we render char by char
69
+ actualStyle =
70
+ actualStyle ||
71
+ this.getCompleteStyleDeclaration(lineIndex, i);
72
+ nextStyle = this.getCompleteStyleDeclaration(lineIndex, i + 1);
73
+ timeToRender = fabric.util.hasStyleChanged(
74
+ actualStyle,
75
+ nextStyle,
76
+ false
77
+ );
78
+ }
79
+ // if (timeToRender) {
80
+ if (path) {
81
+ ctx.save();
82
+ ctx.translate(charBox.renderLeft, charBox.renderTop);
83
+ ctx.rotate(charBox.angle);
84
+ this._renderChar(
85
+ method,
86
+ ctx,
87
+ lineIndex,
88
+ i,
89
+ charsToRender,
90
+ -boxWidth / 2,
91
+ 0,
92
+ lineHeight
93
+ );
94
+ ctx.restore();
95
+ } else {
96
+ drawingLeft = left;
97
+ this._renderChar(
98
+ method,
99
+ ctx,
100
+ lineIndex,
101
+ i,
102
+ charsToRender,
103
+ drawingLeft,
104
+ top,
105
+ lineHeight
106
+ );
107
+ }
108
+ charsToRender = '';
109
+ actualStyle = nextStyle;
110
+ left += sign * boxWidth;
111
+ boxWidth = 0;
112
+ // }
113
+ }
114
+ ctx.restore();
115
+ },
116
+ enlargeSpaces: function () {
117
+ var diffSpace,
118
+ currentLineWidth,
119
+ numberOfSpaces,
120
+ accumulatedSpace,
121
+ line,
122
+ charBound,
123
+ spaces;
124
+ for (var i = 0, len = this._textLines.length; i < len; i++) {
125
+ if (
126
+ this.textAlign !== 'justify' &&
127
+ (i === len - 1 || this.isEndOfWrapping(i))
128
+ ) {
129
+ continue;
130
+ }
131
+ accumulatedSpace = 0;
132
+ line = this._textLines[i];
133
+ currentLineWidth = this.getLineWidth(i);
134
+ if (
135
+ currentLineWidth < this.width &&
136
+ (spaces = this.textLines[i].split('')) &&
137
+ spaces.length > 1
138
+ ) {
139
+ numberOfSpaces = spaces.length;
140
+ diffSpace =
141
+ (this.width - currentLineWidth) / (numberOfSpaces - 1);
142
+ for (var j = 0, jlen = line.length; j <= jlen; j++) {
143
+ charBound = this.__charBounds[i][j];
144
+ // if (this._reSpaceAndTab.test(line[j])) {
145
+ charBound.left += accumulatedSpace;
146
+ if (j < jlen - 1) {
147
+ charBound.width += diffSpace;
148
+ charBound.kernedWidth += diffSpace;
149
+ accumulatedSpace += diffSpace;
150
+ }
151
+ // } else {
152
+ // charBound.left += accumulatedSpace;
153
+ // }
154
+ }
155
+ }
156
+ }
157
+ },
158
+ });
159
+
160
+ fabric.Textbox.fromObject = function (options, callback) {
161
+ const { text } = options;
162
+ return callback(new fabric.Textbox(text, options));
163
+ };
164
+
165
+ export default fabric.Rect;
package/src/plugin.ts CHANGED
@@ -1,88 +1,88 @@
1
- import Editor from './Editor';
2
- type IEditor = Editor;
3
-
4
- class FontPlugin {
5
- public canvas: fabric.Canvas;
6
- public editor: IEditor;
7
- // 插件名称
8
- static pluginName = 'FontPlugin';
9
- // 挂载API名称
10
- static apis = ['downFontByJSON'];
11
- // 发布事件
12
- static events = ['textEvent1', 'textEvent2'];
13
- // 快捷键 keyCode hotkeys-js
14
- public hotkeys: string[] = ['backspace', 'space'];
15
- // 私有属性
16
- repoSrc: string;
17
-
18
- constructor(
19
- canvas: fabric.Canvas,
20
- editor: IEditor,
21
- config: { repoSrc: string }
22
- ) {
23
- // 初始化
24
- this.canvas = canvas;
25
- this.editor = editor;
26
- // 可插入外部配置
27
- this.repoSrc = config.repoSrc;
28
- }
29
-
30
- // 钩子函数 hookImportAfter/hookSaveBefore/hookSaveAfter Promise
31
- hookImportBefore(json: string) {
32
- return this.downFontByJSON(json);
33
- }
34
-
35
- // 挂载API方法
36
- downFontByJSON() {
37
- //
38
- }
39
-
40
- // 私有方法 + 发布事件
41
- _createFontCSS() {
42
- const params = [];
43
- this.editor.emit('textEvent1', params);
44
- }
45
-
46
- // 右键菜单
47
- contextMenu() {
48
- const selectedMode = this.editor.getSelectMode();
49
- if (selectedMode === SelectMode.ONE) {
50
- return [
51
- null, // 分割线
52
- {
53
- text: '翻转',
54
- hotkey: '❯',
55
- subitems: [
56
- {
57
- text: '水平翻转',
58
- hotkey: '|',
59
- onclick: () => this.flip('X'),
60
- },
61
- {
62
- text: '垂直翻转',
63
- hotkey: '-',
64
- onclick: () => this.flip('Y'),
65
- },
66
- ],
67
- },
68
- ];
69
- }
70
- }
71
-
72
- // 快捷键
73
- hotkeyEvent(eventName: string, { type }: KeyboardEvent) {
74
- // eventName:hotkeys中的属性 backspace、space
75
- // type:keyUp keyDown
76
- // code:hotkeys-js Code
77
- if (eventName === 'backspace' && type === 'keydown') {
78
- this.del();
79
- }
80
- }
81
-
82
- // 注销
83
- destroy() {
84
- console.log('pluginDestroy');
85
- }
86
- }
87
-
88
- export default FontPlugin;
1
+ import Editor from './Editor';
2
+ type IEditor = Editor;
3
+
4
+ class FontPlugin {
5
+ public canvas: fabric.Canvas;
6
+ public editor: IEditor;
7
+ // 插件名称
8
+ static pluginName = 'FontPlugin';
9
+ // 挂载API名称
10
+ static apis = ['downFontByJSON'];
11
+ // 发布事件
12
+ static events = ['textEvent1', 'textEvent2'];
13
+ // 快捷键 keyCode hotkeys-js
14
+ public hotkeys: string[] = ['backspace', 'space'];
15
+ // 私有属性
16
+ repoSrc: string;
17
+
18
+ constructor(
19
+ canvas: fabric.Canvas,
20
+ editor: IEditor,
21
+ config: { repoSrc: string }
22
+ ) {
23
+ // 初始化
24
+ this.canvas = canvas;
25
+ this.editor = editor;
26
+ // 可插入外部配置
27
+ this.repoSrc = config.repoSrc;
28
+ }
29
+
30
+ // 钩子函数 hookImportAfter/hookSaveBefore/hookSaveAfter Promise
31
+ hookImportBefore(json: string) {
32
+ return this.downFontByJSON(json);
33
+ }
34
+
35
+ // 挂载API方法
36
+ downFontByJSON() {
37
+ //
38
+ }
39
+
40
+ // 私有方法 + 发布事件
41
+ _createFontCSS() {
42
+ const params = [];
43
+ this.editor.emit('textEvent1', params);
44
+ }
45
+
46
+ // 右键菜单
47
+ contextMenu() {
48
+ const selectedMode = this.editor.getSelectMode();
49
+ if (selectedMode === SelectMode.ONE) {
50
+ return [
51
+ null, // 分割线
52
+ {
53
+ text: '翻转',
54
+ hotkey: '❯',
55
+ subitems: [
56
+ {
57
+ text: '水平翻转',
58
+ hotkey: '|',
59
+ onclick: () => this.flip('X'),
60
+ },
61
+ {
62
+ text: '垂直翻转',
63
+ hotkey: '-',
64
+ onclick: () => this.flip('Y'),
65
+ },
66
+ ],
67
+ },
68
+ ];
69
+ }
70
+ }
71
+
72
+ // 快捷键
73
+ hotkeyEvent(eventName: string, { type }: KeyboardEvent) {
74
+ // eventName:hotkeys中的属性 backspace、space
75
+ // type:keyUp keyDown
76
+ // code:hotkeys-js Code
77
+ if (eventName === 'backspace' && type === 'keydown') {
78
+ this.del();
79
+ }
80
+ }
81
+
82
+ // 注销
83
+ destroy() {
84
+ console.log('pluginDestroy');
85
+ }
86
+ }
87
+
88
+ export default FontPlugin;