Skip to content

Commit bf892d9

Browse files
committed
chore(shape): update shape position (move or resize) by given offset
1 parent 945d7fd commit bf892d9

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

src/helper/modify-shape-helper.ts

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ import { GeneralHelper } from './general-helper';
44
import TextReplaceHelper from './text-replace-helper';
55
import ModifyTextHelper from './modify-text-helper';
66

7+
const map = {
8+
x: { tag: 'a:off', attribute: 'x' },
9+
l: { tag: 'a:off', attribute: 'x' },
10+
left: { tag: 'a:off', attribute: 'x' },
11+
y: { tag: 'a:off', attribute: 'y' },
12+
t: { tag: 'a:off', attribute: 'y' },
13+
top: { tag: 'a:off', attribute: 'y' },
14+
cx: { tag: 'a:ext', attribute: 'cx' },
15+
w: { tag: 'a:ext', attribute: 'cx' },
16+
width: { tag: 'a:ext', attribute: 'cx' },
17+
cy: { tag: 'a:ext', attribute: 'cy' },
18+
h: { tag: 'a:ext', attribute: 'cy' },
19+
height: { tag: 'a:ext', attribute: 'cy' },
20+
};
21+
722
export default class ModifyShapeHelper {
823
/**
924
* Set solid fill of modified shape
@@ -18,55 +33,67 @@ export default class ModifyShapeHelper {
1833
/**
1934
* Set text content of modified shape
2035
*/
21-
static setText = (text: string) => (element: XMLDocument | Element): void => {
22-
ModifyTextHelper.setText(text)(element as Element)
23-
};
36+
static setText =
37+
(text: string) =>
38+
(element: XMLDocument | Element): void => {
39+
ModifyTextHelper.setText(text)(element as Element);
40+
};
2441

2542
/**
2643
* Replace tagged text content within modified shape
2744
*/
28-
static replaceText = (
29-
replaceText: ReplaceText | ReplaceText[],
30-
options?: ReplaceTextOptions,
31-
) => (element: XMLDocument | Element): void => {
32-
const replaceTexts = GeneralHelper.arrayify(replaceText);
45+
static replaceText =
46+
(replaceText: ReplaceText | ReplaceText[], options?: ReplaceTextOptions) =>
47+
(element: XMLDocument | Element): void => {
48+
const replaceTexts = GeneralHelper.arrayify(replaceText);
3349

34-
new TextReplaceHelper(options, element as XMLDocument)
35-
.isolateTaggedNodes()
36-
.applyReplacements(replaceTexts);
37-
};
50+
new TextReplaceHelper(options, element as XMLDocument)
51+
.isolateTaggedNodes()
52+
.applyReplacements(replaceTexts);
53+
};
3854

3955
/**
4056
* Set position and size of modified shape.
4157
*/
42-
static setPosition = (pos: ShapeCoordinates) => (
43-
element: XMLDocument | Element,
44-
): void => {
45-
const map = {
46-
x: { tag: 'a:off', attribute: 'x' },
47-
l: { tag: 'a:off', attribute: 'x' },
48-
left: { tag: 'a:off', attribute: 'x' },
49-
y: { tag: 'a:off', attribute: 'y' },
50-
t: { tag: 'a:off', attribute: 'y' },
51-
top: { tag: 'a:off', attribute: 'y' },
52-
cx: { tag: 'a:ext', attribute: 'cx' },
53-
w: { tag: 'a:ext', attribute: 'cx' },
54-
width: { tag: 'a:ext', attribute: 'cx' },
55-
cy: { tag: 'a:ext', attribute: 'cy' },
56-
h: { tag: 'a:ext', attribute: 'cy' },
57-
height: { tag: 'a:ext', attribute: 'cy' },
58+
static setPosition =
59+
(pos: ShapeCoordinates) =>
60+
(element: XMLDocument | Element): void => {
61+
const xfrm = element.getElementsByTagName('a:off')[0]
62+
.parentNode as Element;
63+
64+
Object.keys(pos).forEach((key) => {
65+
let value = Math.round(pos[key]);
66+
if (typeof value !== 'number' || !map[key]) return;
67+
value = value < 0 ? 0 : value;
68+
69+
xfrm
70+
.getElementsByTagName(map[key].tag)[0]
71+
.setAttribute(map[key].attribute, value);
72+
});
5873
};
5974

60-
const xfrm = element.getElementsByTagName('a:off')[0].parentNode as Element;
75+
/**
76+
* Update position and size of a shape by a given Value.
77+
*/
78+
static updatePosition =
79+
(pos: ShapeCoordinates) =>
80+
(element: XMLDocument | Element): void => {
81+
const xfrm = element.getElementsByTagName('a:off')[0]
82+
.parentNode as Element;
6183

62-
Object.keys(pos).forEach((key) => {
63-
let value = Math.round(pos[key]);
64-
if(typeof value !== 'number' || !map[key]) return;
65-
value = (value < 0) ? 0 : value
84+
Object.keys(pos).forEach((key) => {
85+
let value = Math.round(pos[key]);
86+
if (typeof value !== 'number' || !map[key]) return;
6687

67-
xfrm
68-
.getElementsByTagName(map[key].tag)[0]
69-
.setAttribute(map[key].attribute, value);
70-
});
71-
};
88+
const currentValue = xfrm
89+
.getElementsByTagName(map[key].tag)[0]
90+
.getAttribute(map[key].attribute);
91+
92+
value += Number(currentValue);
93+
94+
xfrm
95+
.getElementsByTagName(map[key].tag)[0]
96+
.setAttribute(map[key].attribute, value);
97+
});
98+
};
7299
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const setSolidFill = ModifyShapeHelper.setSolidFill;
1010
const setText = ModifyShapeHelper.setText;
1111
const replaceText = ModifyShapeHelper.replaceText;
1212
const setPosition = ModifyShapeHelper.setPosition;
13+
const updatePosition = ModifyShapeHelper.updatePosition;
1314

1415
import ModifyTableHelper from './helper/modify-table-helper';
1516
const setTableData = ModifyTableHelper.setTableData;
@@ -82,6 +83,7 @@ export const modify = {
8283
setText,
8384
replaceText,
8485
setPosition,
86+
updatePosition,
8587
setTableData,
8688
adjustHeight,
8789
adjustWidth,

0 commit comments

Comments
 (0)