@hprint/core 0.0.1-alpha.3 → 0.0.1-alpha.4

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,58 +1,58 @@
1
- import type Editor from '@hprint/core';
2
-
3
- // IEditor类型包含插件实例,Editor不包含插件实例
4
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
5
- export interface IEditor extends Editor { }
6
-
7
- // 生命周期事件类型
8
- export type IEditorHooksType =
9
- | 'hookImportBefore'
10
- | 'hookImportAfter'
11
- | 'hookSaveBefore'
12
- | 'hookSaveAfter'
13
- | 'hookTransform'
14
- | 'hookTransformObjectEnd';
15
-
16
- // 插件实例
17
- export declare class IPluginTempl {
18
- constructor(
19
- canvas: fabric.Canvas,
20
- editor: IEditor,
21
- options?: IPluginOption
22
- );
23
- static pluginName: string;
24
- static events: string[];
25
- static apis: string[];
26
- hotkeyEvent?: (name: string, e: KeyboardEvent) => void;
27
- hookImportBefore?: (...args: unknown[]) => Promise<unknown>;
28
- hookImportAfter?: (...args: unknown[]) => Promise<unknown>;
29
- hookSaveBefore?: (...args: unknown[]) => Promise<unknown>;
30
- hookSaveAfter?: (...args: unknown[]) => Promise<unknown>;
31
- hookTransform?: (...args: unknown[]) => Promise<unknown>;
32
- hookTransformObjectEnd?: (...args: unknown[]) => Promise<unknown>;
33
- [propName: string]: any;
34
- canvas?: fabric.Canvas;
35
- editor?: IEditor;
36
- }
37
-
38
- export declare interface IPluginOption {
39
- [propName: string]: unknown | undefined;
40
- }
41
-
42
- declare class IPluginClass2 extends IPluginTempl {
43
- constructor();
44
- }
45
- // 插件class
46
- export declare interface IPluginClass {
47
- new(
48
- canvas: fabric.Canvas,
49
- editor: Editor,
50
- options?: IPluginOption
51
- ): IPluginClass2;
52
- }
53
-
54
- export declare interface IPluginMenu {
55
- text: string;
56
- command?: () => void;
57
- child?: IPluginMenu[];
58
- }
1
+ import type Editor from '@hprint/core';
2
+
3
+ // IEditor类型包含插件实例,Editor不包含插件实例
4
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
5
+ export interface IEditor extends Editor { }
6
+
7
+ // 生命周期事件类型
8
+ export type IEditorHooksType =
9
+ | 'hookImportBefore'
10
+ | 'hookImportAfter'
11
+ | 'hookSaveBefore'
12
+ | 'hookSaveAfter'
13
+ | 'hookTransform'
14
+ | 'hookTransformObjectEnd';
15
+
16
+ // 插件实例
17
+ export declare class IPluginTempl {
18
+ constructor(
19
+ canvas: fabric.Canvas,
20
+ editor: IEditor,
21
+ options?: IPluginOption
22
+ );
23
+ static pluginName: string;
24
+ static events: string[];
25
+ static apis: string[];
26
+ hotkeyEvent?: (name: string, e: KeyboardEvent) => void;
27
+ hookImportBefore?: (...args: unknown[]) => Promise<unknown>;
28
+ hookImportAfter?: (...args: unknown[]) => Promise<unknown>;
29
+ hookSaveBefore?: (...args: unknown[]) => Promise<unknown>;
30
+ hookSaveAfter?: (...args: unknown[]) => Promise<unknown>;
31
+ hookTransform?: (...args: unknown[]) => Promise<unknown>;
32
+ hookTransformObjectEnd?: (...args: unknown[]) => Promise<unknown>;
33
+ [propName: string]: any;
34
+ canvas?: fabric.Canvas;
35
+ editor?: IEditor;
36
+ }
37
+
38
+ export declare interface IPluginOption {
39
+ [propName: string]: unknown | undefined;
40
+ }
41
+
42
+ declare class IPluginClass2 extends IPluginTempl {
43
+ constructor();
44
+ }
45
+ // 插件class
46
+ export declare interface IPluginClass {
47
+ new(
48
+ canvas: fabric.Canvas,
49
+ editor: Editor,
50
+ options?: IPluginOption
51
+ ): IPluginClass2;
52
+ }
53
+
54
+ export declare interface IPluginMenu {
55
+ text: string;
56
+ command?: () => void;
57
+ child?: IPluginMenu[];
58
+ }
@@ -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;