@tmagic/editor 1.0.6 → 1.1.0-beta.2

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.
@@ -81,21 +81,17 @@ export const getNodeIndex = (node: MNode, parent: MContainer | MApp): number =>
81
81
  return items.findIndex((item: MNode) => `${item.id}` === `${node.id}`);
82
82
  };
83
83
 
84
- export const toRelative = (node: MNode) => {
85
- node.style = {
86
- ...(node.style || {}),
87
- position: 'relative',
88
- top: 0,
89
- left: 0,
90
- };
91
- return node;
92
- };
93
-
94
- const setTop2Middle = (node: MNode, parentNode: MNode, stage: StageCore) => {
95
- const style = node.style || {};
84
+ export const getRelativeStyle = (style: Record<string, any> = {}): Record<string, any> => ({
85
+ ...style,
86
+ position: 'relative',
87
+ top: 0,
88
+ left: 0,
89
+ });
90
+
91
+ const getMiddleTop = (style: Record<string, any> = {}, parentNode: MNode, stage: StageCore) => {
96
92
  let height = style.height || 0;
97
93
 
98
- if (!stage || typeof style.top !== 'undefined' || !parentNode.style) return style;
94
+ if (!stage || typeof style.top !== 'undefined' || !parentNode.style) return style.top;
99
95
 
100
96
  if (!isNumber(height)) {
101
97
  height = 0;
@@ -105,43 +101,45 @@ const setTop2Middle = (node: MNode, parentNode: MNode, stage: StageCore) => {
105
101
 
106
102
  if (isPage(parentNode)) {
107
103
  const { scrollTop = 0, wrapperHeight } = stage.mask;
108
- style.top = (wrapperHeight - height) / 2 + scrollTop;
109
- } else {
110
- style.top = (parentHeight - height) / 2;
104
+ return (wrapperHeight - height) / 2 + scrollTop;
111
105
  }
112
-
113
- return style;
106
+ return (parentHeight - height) / 2;
114
107
  };
115
108
 
116
- export const initPosition = (node: MNode, layout: Layout, parentNode: MNode, stage: StageCore) => {
109
+ export const getInitPositionStyle = (
110
+ style: Record<string, any> = {},
111
+ layout: Layout,
112
+ parentNode: MNode,
113
+ stage: StageCore,
114
+ ) => {
117
115
  if (layout === Layout.ABSOLUTE) {
118
- node.style = {
116
+ return {
117
+ ...style,
119
118
  position: 'absolute',
120
- ...setTop2Middle(node, parentNode, stage),
119
+ top: getMiddleTop(style, parentNode, stage),
121
120
  };
122
- return node;
123
121
  }
124
122
 
125
123
  if (layout === Layout.RELATIVE) {
126
- return toRelative(node);
124
+ return getRelativeStyle(style);
127
125
  }
128
126
 
129
- return node;
127
+ return style;
130
128
  };
131
129
 
132
130
  export const setLayout = (node: MNode, layout: Layout) => {
133
131
  node.items?.forEach((child: MNode) => {
134
132
  if (isPop(child)) return;
135
133
 
136
- child.style = child.style || {};
134
+ const style = child.style || {};
137
135
 
138
136
  // 是 fixed 不做处理
139
- if (child.style.position === 'fixed') return;
137
+ if (style.position === 'fixed') return;
140
138
 
141
139
  if (layout !== Layout.RELATIVE) {
142
- child.style.position = 'absolute';
140
+ style.position = 'absolute';
143
141
  } else {
144
- toRelative(child);
142
+ child.style = getRelativeStyle(style);
145
143
  child.style.right = 'auto';
146
144
  child.style.bottom = 'auto';
147
145
  }
@@ -161,11 +159,10 @@ export const change2Fixed = (node: MNode, root: MApp) => {
161
159
  offset.top = offset.top + globalThis.parseFloat(value.style?.top || 0);
162
160
  });
163
161
 
164
- node.style = {
162
+ return {
165
163
  ...(node.style || {}),
166
164
  ...offset,
167
165
  };
168
- return node;
169
166
  };
170
167
 
171
168
  export const Fixed2Other = async (
@@ -186,23 +183,23 @@ export const Fixed2Other = async (
186
183
  offset.left = offset.left - globalThis.parseFloat(value.style?.left || 0);
187
184
  offset.top = offset.top - globalThis.parseFloat(value.style?.top || 0);
188
185
  });
186
+ const style = node.style || {};
189
187
 
190
188
  const parent = path.pop();
191
189
  if (!parent) {
192
- return toRelative(node);
190
+ return getRelativeStyle(style);
193
191
  }
194
192
 
195
193
  const layout = await getLayout(parent);
196
194
  if (layout !== Layout.RELATIVE) {
197
- node.style = {
198
- ...(node.style || {}),
195
+ return {
196
+ ...style,
199
197
  ...offset,
200
198
  position: 'absolute',
201
199
  };
202
- return node;
203
200
  }
204
201
 
205
- return toRelative(node);
202
+ return getRelativeStyle(style);
206
203
  };
207
204
 
208
205
  export const getGuideLineFromCache = (key: string): number[] => {
@@ -219,3 +216,16 @@ export const getGuideLineFromCache = (key: string): number[] => {
219
216
 
220
217
  return [];
221
218
  };
219
+
220
+ export const fixNodeLeft = (config: MNode, parent: MContainer, doc?: Document) => {
221
+ if (!doc || !config.style || !isNumber(config.style.left)) return config.style?.left;
222
+
223
+ const el = doc.getElementById(`${config.id}`);
224
+ const parentEl = doc.getElementById(`${parent.id}`);
225
+
226
+ if (el && parentEl && el.offsetWidth + config.style?.left > parentEl.offsetWidth) {
227
+ return parentEl.offsetWidth - el.offsetWidth;
228
+ }
229
+
230
+ return config.style.left;
231
+ };