Skip to content

Commit 36f65a0

Browse files
committed
chore: simplify placeholder matching; fix replaceInto selector for multiple equal shape names
1 parent 36a2407 commit 36f65a0

File tree

9 files changed

+162
-155
lines changed

9 files changed

+162
-155
lines changed

src/classes/has-shapes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ export default class HasShapes {
592592
return {
593593
mode: importElement.mode,
594594
name: selector,
595+
selector: XmlSlideHelper.getSelector(sourceElement),
595596
hasCreationId: mode === 'findByElementCreationId',
596597
sourceArchive,
597598
sourceSlideNumber: slideNumber,

src/classes/shape.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { XmlHelper } from '../helper/xml-helper';
2-
import { GeneralHelper, vd } from '../helper/general-helper';
2+
import { GeneralHelper } from '../helper/general-helper';
33
import { HyperlinkProcessor } from '../helper/hyperlink-processor';
44
import {
55
ChartModificationCallback,
@@ -11,7 +11,10 @@ import {
1111
} from '../types/types';
1212
import { RootPresTemplate } from '../interfaces/root-pres-template';
1313
import { XmlDocument, XmlElement } from '../types/xml-types';
14-
import { ContentTypeExtension, ContentTypeMap } from '../enums/content-type-map';
14+
import {
15+
ContentTypeExtension,
16+
ContentTypeMap,
17+
} from '../enums/content-type-map';
1518
import { ElementSubtype } from '../enums/element-type';
1619
import IArchive from '../interfaces/iarchive';
1720

@@ -121,7 +124,10 @@ export class Shape {
121124
async processHyperlinks(): Promise<void> {
122125
if (!this.targetElement || !this.createdRid) return;
123126

124-
await HyperlinkProcessor.processSingleHyperlink(this.targetElement, this.createdRid);
127+
await HyperlinkProcessor.processSingleHyperlink(
128+
this.targetElement,
129+
this.createdRid,
130+
);
125131
}
126132

127133
async replaceIntoSlideTree(): Promise<void> {
@@ -142,10 +148,14 @@ export class Shape {
142148
);
143149

144150
const findMethod = this.hasCreationId ? 'findByCreationId' : 'findByName';
151+
const selector = this.hasCreationId
152+
? this.shape.selector.creationId
153+
: this.shape.selector.name;
145154

146155
const sourceElementOnTargetSlide = await XmlHelper[findMethod](
147156
targetSlideXml,
148-
this.name,
157+
selector,
158+
this.shape.selector.nameIdx,
149159
);
150160

151161
if (!sourceElementOnTargetSlide?.parentNode) {
@@ -172,7 +182,9 @@ export class Shape {
172182
XmlHelper.writeXmlToArchive(archive, slideFile, targetSlideXml);
173183
}
174184

175-
async updateElementsRelId(cb?: (targetElement: XmlElement) => void): Promise<void> {
185+
async updateElementsRelId(
186+
cb?: (targetElement: XmlElement) => void,
187+
): Promise<void> {
176188
const targetSlideXml = await XmlHelper.getXmlFromArchive(
177189
this.targetArchive,
178190
this.targetSlideFile,
@@ -184,8 +196,8 @@ export class Shape {
184196
);
185197

186198
targetElements.forEach((targetElement: XmlElement) => {
187-
if(cb && typeof cb === 'function') {
188-
cb(targetElement)
199+
if (cb && typeof cb === 'function') {
200+
cb(targetElement);
189201
} else {
190202
this.relParent(targetElement)
191203
.getElementsByTagName(this.relRootTag)[0]
@@ -212,11 +224,7 @@ export class Shape {
212224
.getElementsByTagName('p:cSld')[0]
213225
.getElementsByTagName(this.relRootTag);
214226

215-
return XmlHelper.findByAttributeValue(
216-
sourceList,
217-
this.relAttribute,
218-
rId,
219-
);
227+
return XmlHelper.findByAttributeValue(sourceList, this.relAttribute, rId);
220228
}
221229

222230
async updateTargetElementRelId(): Promise<void> {

src/helper/modify-cleanup-helper.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ export default class ModifyCleanupHelper {
143143
* @param element - The XML element to clean up
144144
*/
145145
static removeEffects(element: XmlElement): void {
146-
this.removeShapeStyle(element);
147-
this.removeColorAdjustment(element);
148-
this.removeShadowEffects(element);
149-
this.remove3dEffects(element);
150-
this.removeFillEffects(element);
151-
this.removeTextEffects(element);
146+
ModifyCleanupHelper.removeShapeStyle(element);
147+
ModifyCleanupHelper.removeColorAdjustment(element);
148+
ModifyCleanupHelper.removeShadowEffects(element);
149+
ModifyCleanupHelper.remove3dEffects(element);
150+
ModifyCleanupHelper.removeFillEffects(element);
151+
ModifyCleanupHelper.removeTextEffects(element);
152152

153153
// Determine the visual type of the element (picture, chart, etc.)
154154
// Apply type-specific cleanup

src/helper/modify-table-helper.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,47 @@ export default class ModifyTableHelper {
8282
const modTable = new ModifyTable(element);
8383
modTable.updateRowHeight(index, size);
8484
};
85+
86+
static setTableStyle =
87+
(styleId: string, attribs: string[]) => (element: XmlElement) => {
88+
const tblPr = element.getElementsByTagName('a:tblPr').item(0);
89+
90+
const setTableStyleId = (tableStyleId: XmlElement, id: string) => {
91+
tableStyleId.textContent = id;
92+
};
93+
94+
const createTableStyleId = (tblPr: XmlElement) => {
95+
const tableStyleId =
96+
tblPr.ownerDocument.createElement('a:tableStyleId');
97+
tblPr.appendChild(tableStyleId);
98+
return tableStyleId;
99+
};
100+
const updateTable = (tblPr: XmlElement) => {
101+
[
102+
'firstRow',
103+
'firstCol',
104+
'lastRow',
105+
'lastCol',
106+
'bandRow',
107+
'bandCol',
108+
].forEach((attrib) => {
109+
if (attribs.includes(attrib)) {
110+
tblPr.setAttribute(attrib, '1');
111+
} else {
112+
tblPr.removeAttribute(attrib);
113+
}
114+
});
115+
116+
const tableStyleId =
117+
element.getElementsByTagName('a:tableStyleId').item(0) ||
118+
createTableStyleId(tblPr);
119+
120+
if (tableStyleId) {
121+
setTableStyleId(tableStyleId, styleId);
122+
}
123+
};
124+
if (tblPr) {
125+
updateTable(tblPr);
126+
}
127+
};
85128
}

src/helper/xml-helper.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ export class XmlHelper {
425425
archive: IArchive,
426426
path: string,
427427
creationId: string,
428-
nameIdx?: number,
429428
): Promise<XmlElement> {
430429
const slideXml = await XmlHelper.getXmlFromArchive(archive, path);
431430

0 commit comments

Comments
 (0)