Skip to content

Commit dc6bfcf

Browse files
committed
refactor(slidedeck): replace nested ternaries with small functions
1 parent 2c7c6f5 commit dc6bfcf

2 files changed

Lines changed: 38 additions & 31 deletions

File tree

‎packages/slidedeck/src/free.ts‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ function shapeXml(element: ShapeElement, part: DrawingPart, opacity: number) {
228228
const SVG_BLIP = (relId: string) =>
229229
`<a:extLst><a:ext uri="{96DAC541-7B7A-43D3-8B79-37D633B846F1}"><asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="${relId}"/></a:ext></a:extLst>`;
230230

231+
function sourceRectXml(element: PictureElement, info: ImageInfo, area: Box) {
232+
if (element.fit === "cover") return coverCrop(info, area);
233+
const { crop } = element;
234+
if (!crop) return "";
235+
return `<a:srcRect l="${percent(crop.left)}" t="${percent(crop.top)}" r="${percent(crop.right)}" b="${percent(crop.bottom)}"/>`;
236+
}
237+
231238
function pictureXml(
232239
element: PictureElement,
233240
part: DrawingPart,
@@ -237,13 +244,6 @@ function pictureXml(
237244
const { info, relId } = part.image(element.image);
238245
const area = boxOf(element);
239246
const box = element.fit === "contain" ? contain(info, area) : area;
240-
const { crop } = element;
241-
const sourceRect =
242-
element.fit === "cover"
243-
? coverCrop(info, area)
244-
: crop
245-
? `<a:srcRect l="${percent(crop.left)}" t="${percent(crop.top)}" r="${percent(crop.right)}" b="${percent(crop.bottom)}"/>`
246-
: "";
247247
const blipChildren = `${opacity < 1 ? `<a:alphaModFix amt="${percent(opacity)}"/>` : ""}${element.svg ? SVG_BLIP(part.svg(element.svg)) : ""}`;
248248
const blip = blipChildren
249249
? `<a:blip r:embed="${relId}">${blipChildren}</a:blip>`
@@ -253,7 +253,7 @@ function pictureXml(
253253
return {
254254
spid,
255255
build: false,
256-
xml: `<p:pic><p:nvPicPr>${cNvPr}<p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr><p:nvPr/></p:nvPicPr><p:blipFill>${blip}${sourceRect}<a:stretch><a:fillRect/></a:stretch></p:blipFill><p:spPr>${xfrm(box, element.rotation)}${geometry}${line}${shadowXml(element.shadow, box, opacity)}</p:spPr></p:pic>`,
256+
xml: `<p:pic><p:nvPicPr>${cNvPr}<p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr><p:nvPr/></p:nvPicPr><p:blipFill>${blip}${sourceRectXml(element, info, area)}<a:stretch><a:fillRect/></a:stretch></p:blipFill><p:spPr>${xfrm(box, element.rotation)}${geometry}${line}${shadowXml(element.shadow, box, opacity)}</p:spPr></p:pic>`,
257257
};
258258
}
259259

@@ -288,6 +288,24 @@ function groupXml(
288288
};
289289
}
290290

291+
function drawnXml(
292+
element: FreeElement,
293+
part: DrawingPart,
294+
targets: Map<string, Target>,
295+
opacity: number,
296+
): Drawn {
297+
switch (element.type) {
298+
case "text":
299+
return textXml(element, part, opacity);
300+
case "shape":
301+
return shapeXml(element, part, opacity);
302+
case "image":
303+
return pictureXml(element, part, opacity);
304+
case "group":
305+
return groupXml(element, part, targets, opacity);
306+
}
307+
}
308+
291309
/** Draws one element, recording the shape id of each element with an `id` for animations. */
292310
export function elementXml(
293311
element: FreeElement,
@@ -296,14 +314,7 @@ export function elementXml(
296314
inherited = 1,
297315
): string {
298316
const opacity = inherited * (element.opacity ?? 1);
299-
const drawn: Drawn =
300-
element.type === "text"
301-
? textXml(element, part, opacity)
302-
: element.type === "shape"
303-
? shapeXml(element, part, opacity)
304-
: element.type === "image"
305-
? pictureXml(element, part, opacity)
306-
: groupXml(element, part, targets, opacity);
317+
const drawn = drawnXml(element, part, targets, opacity);
307318
if (element.id !== undefined)
308319
targets.set(element.id, { spid: drawn.spid, build: drawn.build });
309320
return drawn.xml;

‎packages/slidedeck/src/motion.ts‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,27 +201,23 @@ function changeEffect(
201201
}
202202
}
203203

204-
const presetClass = (animation: Animation) =>
205-
"kind" in animation
206-
? animation.kind === "entrance"
207-
? "entr"
208-
: "exit"
209-
: animation.effect === "path"
210-
? "path"
211-
: "emph";
204+
function presetClass(animation: Animation) {
205+
if ("kind" in animation)
206+
return animation.kind === "entrance" ? "entr" : "exit";
207+
return animation.effect === "path" ? "path" : "emph";
208+
}
209+
210+
function presetSubtype(animation: Animation) {
211+
if ("side" in animation) return SUBTYPES[animation.side];
212+
return animation.effect === "zoom" ? ZOOM_FROM_CENTER : 0;
213+
}
212214

213215
function effectXml(
214216
timeline: Timeline,
215217
animation: Animation,
216218
target: Target,
217219
nodeType: string,
218220
) {
219-
const subtype =
220-
"side" in animation
221-
? SUBTYPES[animation.side]
222-
: animation.effect === "zoom"
223-
? ZOOM_FROM_CENTER
224-
: 0;
225221
const easing = animation.easing
226222
? `${animation.easing.accel > 0 ? ` accel="${Math.round(animation.easing.accel * 100000)}"` : ""}${animation.easing.decel > 0 ? ` decel="${Math.round(animation.easing.decel * 100000)}"` : ""}`
227223
: "";
@@ -231,7 +227,7 @@ function effectXml(
231227
: ` repeatCount="${animation.repeat === "indefinite" ? "indefinite" : Math.round(animation.repeat * 1000)}"`;
232228
const id = timeline.id();
233229
const grpId = timeline.grpId(target);
234-
return `<p:par><p:cTn id="${id}" presetID="${PRESETS[animation.effect]}" presetClass="${presetClass(animation)}" presetSubtype="${subtype}"${easing}${repeat} fill="hold" grpId="${grpId}" nodeType="${nodeType}"><p:stCondLst><p:cond delay="${animation.delay}"/></p:stCondLst><p:childTnLst>${behaviors(timeline, animation, target.spid)}</p:childTnLst></p:cTn></p:par>`;
230+
return `<p:par><p:cTn id="${id}" presetID="${PRESETS[animation.effect]}" presetClass="${presetClass(animation)}" presetSubtype="${presetSubtype(animation)}"${easing}${repeat} fill="hold" grpId="${grpId}" nodeType="${nodeType}"><p:stCondLst><p:cond delay="${animation.delay}"/></p:stCondLst><p:childTnLst>${behaviors(timeline, animation, target.spid)}</p:childTnLst></p:cTn></p:par>`;
235231
}
236232

237233
const playTime = (animation: Animation) =>

0 commit comments

Comments
 (0)