Skip to content

Commit

Permalink
Fix for Issue #640
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbrent committed Apr 17, 2021
1 parent c590105 commit f826a93
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
7 changes: 3 additions & 4 deletions demos/modules/demo_master.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function genSlide01(pptx) {
function genSlide02(pptx) {
let slide = pptx.addSlide({ masterName: "MASTER_SLIDE", sectionTitle: "Masters" });
slide.addNotes("Master name: `MASTER_SLIDE`\nAPI Docs: https://gitbrent.github.io/PptxGenJS/docs/masters.html");
slide.addText("", { placeholder: "title" });
}

/**
Expand All @@ -48,7 +47,7 @@ function genSlide02(pptx) {
function genSlide03(pptx) {
let slide = pptx.addSlide({ masterName: "MASTER_SLIDE", sectionTitle: "Masters" });
slide.addNotes("Master name: `MASTER_SLIDE` using pre-filled placeholders\nAPI Docs: https://gitbrent.github.io/PptxGenJS/docs/masters.html");
slide.addText("Text Placeholder", { placeholder: "title" });
slide.addText("Text Placeholder", { placeholder: "header" });
slide.addText(
[
{ text: "Pre-filled placeholder bullets", options: { bullet: true, valign: "top" } },
Expand All @@ -66,7 +65,7 @@ function genSlide03(pptx) {
function genSlide04(pptx) {
let slide = pptx.addSlide({ masterName: "MASTER_SLIDE", sectionTitle: "Masters" });
slide.addNotes("Master name: `MASTER_SLIDE` using pre-filled placeholders\nAPI Docs: https://gitbrent.github.io/PptxGenJS/docs/masters.html");
slide.addText("Image Placeholder", { placeholder: "title" });
slide.addText("Image Placeholder", { placeholder: "header" });
slide.addImage({
placeholder: "body",
path: IMAGE_PATHS.starlabsBkgd.path,
Expand All @@ -87,7 +86,7 @@ function genSlide05(pptx) {
];
let slide = pptx.addSlide({ masterName: "MASTER_SLIDE", sectionTitle: "Masters" });
slide.addNotes("Master name: `MASTER_SLIDE` using pre-filled placeholders\nAPI Docs: https://gitbrent.github.io/PptxGenJS/docs/masters.html");
slide.addText("Chart Placeholder", { placeholder: "title" });
slide.addText("Chart Placeholder", { placeholder: "header" });
slide.addChart(pptx.charts.PIE, dataChartPieLocs, { showLegend: true, legendPos: "l", placeholder: "body" });
}

Expand Down
22 changes: 18 additions & 4 deletions demos/modules/masters.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ export function createMasterSlides(pptx) {
margin: [0.5, 0.25, 1.0, 0.25],
slideNumber: { x: 0.6, y: 7.1, color: "FFFFFF", fontFace: "Arial", fontSize: 10 },
objects: [
{ rect: { x: 0.0, y: 6.9, w: "100%", h: 0.6, fill: { color: "003b75" } } },
//{ 'image': { x:11.45, y:5.95, w:1.67, h:0.75, data:STARLABS_LOGO_SM } },
{
rect: { x: 0.0, y: 6.9, w: "100%", h: 0.6, fill: { color: "003b75" } },
},
{
text: {
options: { x: 0, y: 6.9, w: "100%", h: 0.6, align: "center", valign: "middle", color: "FFFFFF", fontSize: 12 },
Expand All @@ -85,13 +87,25 @@ export function createMasterSlides(pptx) {
},
{
placeholder: {
options: { name: "title", type: "title", x: 0.6, y: 0.2, w: 12, h: 1.0 },
text: "",
options: {
name: "header",
type: "title",
x: 0.6,
y: 0.2,
w: 12,
h: 1.0,
margin: 0,
align: "center",
valign: "middle",
color: "404040",
//fontSize: 18,
},
text: "", // USAGE: Leave blank to have powerpoint substitute default placeholder text (ex: "Click to add title")
},
},
{
placeholder: {
options: { name: "body", type: "body", x: 0.6, y: 1.5, w: 12, h: 5.25 },
options: { name: "body", type: "body", x: 0.6, y: 1.5, w: 12, h: 5.25, fontSize: 28 },
text: "(supports custom placeholder text!)",
},
},
Expand Down
12 changes: 10 additions & 2 deletions src/gen-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ export function addTableDefinition(
* @param {PresSlide} target - slide object that the text should be added to
* @param {string|TextProps[]} text text string or object
* @param {TextPropsOptions} opts text options
* @param {boolean} isPlaceholder` is this a placeholder object
* @param {boolean} isPlaceholder whether this a placeholder object
* @since: 1.0.0
*/
export function addTextDefinition(target: PresSlide, text: TextProps[], opts: TextPropsOptions, isPlaceholder: boolean) {
Expand All @@ -918,6 +918,14 @@ export function addTextDefinition(target: PresSlide, text: TextProps[], opts: Te
itemOpts.bullet = itemOpts.bullet || false
}

// A.3: Text targeting a placeholder need to inherit the placeholders options (eg: margin, valign, etc.) (Issue #640)
if (itemOpts.placeholder && target._slideLayout && target._slideLayout._slideObjects) {
let placeHold = target._slideLayout._slideObjects.filter(
item => item._type === 'placeholder' && item.options && item.options.placeholder && item.options.placeholder === itemOpts.placeholder
)[0]
if (placeHold && placeHold.options) itemOpts = { ...itemOpts, ...placeHold.options }
}

// B:
if (itemOpts.shape === SHAPE_TYPE.LINE) {
// ShapeLineProps defaults
Expand Down Expand Up @@ -1011,7 +1019,7 @@ export function addPlaceholdersToSlideLayouts(slide: PresSlide) {
// NOTE: Check to ensure a placeholder does not already exist on the Slide
// They are created when they have been populated with text (ex: `slide.addText('Hi', { placeholder:'title' });`)
if (slide._slideObjects.filter(slideObj => slideObj.options && slideObj.options.placeholder === slideLayoutObj.options.placeholder).length === 0) {
addTextDefinition(slide, [{ text: '' }], { placeholder: slideLayoutObj.options.placeholder }, false)
addTextDefinition(slide, [{ text: '' }], slideLayoutObj.options, false)
}
}
})
Expand Down
11 changes: 10 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,16 @@ declare namespace PptxGenJS {
title: string
margin?: Margin
background?: BackgroundProps
objects?: ({ chart: {} } | { image: {} } | { line: {} } | { rect: {} } | { text: TextProps } | { placeholder: { options: PlaceholderProps; text?: string } })[]
objects?: ({ chart: {} } | { image: {} } | { line: {} } | { rect: {} } | { text: TextProps } | {
placeholder: {
options: PlaceholderProps;
/**
* Text to be shown in placeholder (shown until user focuses textbox or adds text)
* - Leave blank to have powerpoint show default phrase (ex: "Click to add title")
*/
text?: string
}
})[]
slideNumber?: SlideNumberProps

/**
Expand Down

0 comments on commit f826a93

Please # to comment.