Skip to content

Commit 7b6cded

Browse files
committed
feat: reuse blockquote block if possible
1 parent 9282bbc commit 7b6cded

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

apply.go

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616
)
1717

1818
const (
19-
styleBlockQuote = "blockquote"
20-
descriptionImageFromMarkdown = "Image generated from markdown"
21-
descriptionTextboxFromMarkdown = "Textbox generated from markdown"
19+
styleBlockQuote = "blockquote"
20+
descriptionImageFromMarkdown = "Image generated from markdown"
21+
descriptionTextboxFromMarkdown = "Textbox generated from markdown"
22+
descriptionBlockquoteTextboxFromMarkdown = "Blockquote textbox generated from markdown"
2223
)
2324

2425
// Apply the markdown slides to the presentation.
@@ -309,6 +310,7 @@ func (d *Deck) prepareToApplyPage(ctx context.Context, index int, slide *Slide,
309310
currentImages []*Image
310311
currentImageObjectIDMap = map[*Image]string{} // key: *Image, value: objectID
311312
currentTextBoxes []*textBox
313+
currentBlockquoteIDs []string
312314
currentTextBoxObjectIDMap = map[*textBox]string{} // key: *textBox, value: objectID
313315
currentTables []*slides.PageElement
314316
)
@@ -373,8 +375,10 @@ func (d *Deck) prepareToApplyPage(ctx context.Context, index int, slide *Slide,
373375
currentImageObjectIDMap[image] = element.ObjectId
374376
case element.Shape != nil && element.Shape.ShapeType == "TEXT_BOX" && element.Shape.Text != nil:
375377
tb := &textBox{}
376-
if element.Description == descriptionTextboxFromMarkdown {
377-
tb.fromMarkdown = true
378+
tb.fromMarkdown = element.Description == descriptionTextboxFromMarkdown ||
379+
element.Description == descriptionBlockquoteTextboxFromMarkdown
380+
if element.Description == descriptionBlockquoteTextboxFromMarkdown {
381+
currentBlockquoteIDs = append(currentBlockquoteIDs, element.ObjectId)
378382
}
379383
tb.paragraphs = convertToParagraphs(element.Shape.Text)
380384
currentTextBoxes = append(currentTextBoxes, tb)
@@ -537,56 +541,70 @@ func (d *Deck) prepareToApplyPage(ctx context.Context, index int, slide *Slide,
537541
}
538542
requests = append(requests, tableRequests...)
539543

544+
reuseBlockquotes := len(currentBlockquoteIDs) == len(slide.BlockQuotes)
540545
// set text boxes
541546
for i, bq := range slide.BlockQuotes {
542-
found := slices.ContainsFunc(currentTextBoxes, func(currentTextBox *textBox) bool {
547+
if slices.ContainsFunc(currentTextBoxes, func(currentTextBox *textBox) bool {
543548
return slices.EqualFunc(currentTextBox.paragraphs, bq.Paragraphs, paragraphEqual)
544-
})
545-
if found {
549+
}) {
546550
continue
547551
}
548-
// create new text box
549-
textBoxObjectID := fmt.Sprintf("textbox-%s", uuid.New().String())
550-
requests = append(requests, &slides.Request{
551-
CreateShape: &slides.CreateShapeRequest{
552-
ObjectId: textBoxObjectID,
553-
ElementProperties: &slides.PageElementProperties{
554-
PageObjectId: currentSlide.ObjectId,
555-
Size: &slides.Size{
556-
Height: &slides.Dimension{
557-
Magnitude: float64(500000 * len(bq.Paragraphs)),
558-
Unit: "EMU",
559-
},
560-
Width: &slides.Dimension{
561-
Magnitude: 5000000,
562-
Unit: "EMU",
563-
},
564-
},
565-
Transform: &slides.AffineTransform{
566-
ScaleX: 1.0,
567-
ScaleY: 1.0,
568-
TranslateX: float64(i+1) * 100000,
569-
TranslateY: float64(i+1) * 100000,
570-
Unit: "EMU",
552+
var textBoxObjectID string
553+
if reuseBlockquotes {
554+
textBoxObjectID = currentBlockquoteIDs[i]
555+
requests = append(requests, &slides.Request{
556+
DeleteText: &slides.DeleteTextRequest{
557+
ObjectId: textBoxObjectID,
558+
TextRange: &slides.Range{
559+
Type: "ALL",
571560
},
572561
},
573-
ShapeType: "TEXT_BOX",
574-
},
575-
})
576-
577-
sp, ok := d.shapes[styleBlockQuote]
578-
if ok {
562+
})
563+
} else {
564+
// create new text box
565+
textBoxObjectID = fmt.Sprintf("textbox-%s", uuid.New().String())
579566
requests = append(requests, &slides.Request{
580-
UpdateShapeProperties: &slides.UpdateShapePropertiesRequest{
581-
ObjectId: textBoxObjectID,
582-
ShapeProperties: sp,
583-
// We want to specify `autofit.autofitType` (such as `SHAPE_AUTOFIT`), but we cannot specify it
584-
// because there is a problem with the Google Slide API.
585-
// See: https://issuetracker.google.com/issues/199176586
586-
Fields: "shapeBackgroundFill,outline,shadow",
567+
CreateShape: &slides.CreateShapeRequest{
568+
ObjectId: textBoxObjectID,
569+
ElementProperties: &slides.PageElementProperties{
570+
PageObjectId: currentSlide.ObjectId,
571+
Size: &slides.Size{
572+
Height: &slides.Dimension{
573+
Magnitude: float64(500000 * len(bq.Paragraphs)),
574+
Unit: "EMU",
575+
},
576+
Width: &slides.Dimension{
577+
Magnitude: 5000000,
578+
Unit: "EMU",
579+
},
580+
},
581+
Transform: &slides.AffineTransform{
582+
ScaleX: 1.0,
583+
ScaleY: 1.0,
584+
TranslateX: float64(i+1) * 100000,
585+
TranslateY: float64(i+1) * 100000,
586+
Unit: "EMU",
587+
},
588+
},
589+
ShapeType: "TEXT_BOX",
587590
},
588591
})
592+
593+
sp, ok := d.shapes[styleBlockQuote]
594+
if ok {
595+
requests = append(requests, &slides.Request{
596+
UpdateShapeProperties: &slides.UpdateShapePropertiesRequest{
597+
ObjectId: textBoxObjectID,
598+
ShapeProperties: sp,
599+
// We want to specify `autofit.autofitType` (such as `SHAPE_AUTOFIT`), but we cannot specify it
600+
// because there is a problem with the Google Slide API.
601+
// See: https://issuetracker.google.com/issues/199176586
602+
Fields: "shapeBackgroundFill,outline,shadow",
603+
},
604+
})
605+
}
589606
}
607+
590608
reqs, styleReqs, err := d.applyParagraphsRequests(textBoxObjectID, bq.Paragraphs)
591609
if err != nil {
592610
return nil, fmt.Errorf("failed to apply paragraphs: %w", err)
@@ -607,7 +625,7 @@ func (d *Deck) prepareToApplyPage(ctx context.Context, index int, slide *Slide,
607625
requests = append(requests, &slides.Request{
608626
UpdatePageElementAltText: &slides.UpdatePageElementAltTextRequest{
609627
ObjectId: textBoxObjectID,
610-
Description: descriptionTextboxFromMarkdown,
628+
Description: descriptionBlockquoteTextboxFromMarkdown,
611629
},
612630
})
613631
}
@@ -650,16 +668,18 @@ func (d *Deck) prepareToApplyPage(ctx context.Context, index int, slide *Slide,
650668
if !currentTextBox.fromMarkdown {
651669
continue
652670
}
653-
found := slices.ContainsFunc(slide.BlockQuotes, func(bq *BlockQuote) bool {
671+
if slices.ContainsFunc(slide.BlockQuotes, func(bq *BlockQuote) bool {
654672
return slices.EqualFunc(currentTextBox.paragraphs, bq.Paragraphs, paragraphEqual)
655-
})
656-
if found {
673+
}) {
657674
continue
658675
}
659676
textBoxObjectID, ok := currentTextBoxObjectIDMap[currentTextBox]
660677
if !ok {
661678
return nil, fmt.Errorf("text box object ID not found for text box: %v", currentTextBox.paragraphs)
662679
}
680+
if reuseBlockquotes && slices.Contains(currentBlockquoteIDs, textBoxObjectID) {
681+
continue
682+
}
663683
requests = append(requests, &slides.Request{
664684
DeleteObject: &slides.DeleteObjectRequest{
665685
ObjectId: textBoxObjectID,

0 commit comments

Comments
 (0)