-
Notifications
You must be signed in to change notification settings - Fork 455
✨(frontend) enable ODT export for documents #1524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ovgodd
wants to merge
1
commit into
main
Choose a base branch
from
feat/export-odf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/frontend/apps/impress/src/features/docs/doc-export/blocks-mapping/calloutODT.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { DocsExporterODT } from '../types'; | ||
| import { odtRegisterParagraphStyleForBlock } from '../utils'; | ||
|
|
||
| export const blockMappingCalloutODT: DocsExporterODT['mappings']['blockMapping']['callout'] = | ||
| (block, exporter) => { | ||
| // Map callout to paragraph with emoji prefix | ||
| const emoji = block.props.emoji || '💡'; | ||
|
|
||
| // Transform inline content (text, bold, links, etc.) | ||
| const inlineContent = exporter.transformInlineContent(block.content); | ||
|
|
||
| // Resolve background and alignment → create a dedicated paragraph style | ||
| const styleName = odtRegisterParagraphStyleForBlock( | ||
| exporter, | ||
| { | ||
| backgroundColor: block.props.backgroundColor, | ||
| textAlignment: block.props.textAlignment, | ||
| }, | ||
| { paddingCm: 0.42 }, | ||
| ); | ||
|
|
||
| return React.createElement( | ||
| 'text:p', | ||
| { | ||
| 'text:style-name': styleName, | ||
| }, | ||
| `${emoji} `, | ||
| ...inlineContent, | ||
| ); | ||
| }; |
128 changes: 128 additions & 0 deletions
128
src/frontend/apps/impress/src/features/docs/doc-export/blocks-mapping/imageODT.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,128 @@ | ||||||
| import React from 'react'; | ||||||
|
|
||||||
| import { DocsExporterODT } from '../types'; | ||||||
| import { convertSvgToPng } from '../utils'; | ||||||
|
|
||||||
| const MAX_WIDTH = 600; | ||||||
|
|
||||||
| export const blockMappingImageODT: DocsExporterODT['mappings']['blockMapping']['image'] = | ||||||
| async (block, exporter) => { | ||||||
| try { | ||||||
| const blob = await exporter.resolveFile(block.props.url); | ||||||
|
|
||||||
| if (!blob || !blob.type) { | ||||||
| console.warn(`Failed to resolve image: ${block.props.url}`); | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| let pngConverted: string | undefined; | ||||||
| let dimensions: { width: number; height: number } | undefined; | ||||||
| let previewWidth = block.props.previewWidth || undefined; | ||||||
|
|
||||||
| if (!blob.type.includes('image')) { | ||||||
| console.warn(`Not an image type: ${blob.type}`); | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| if (blob.type.includes('svg')) { | ||||||
| const svgText = await blob.text(); | ||||||
| const FALLBACK_SIZE = 536; | ||||||
| previewWidth = previewWidth || blob.size || FALLBACK_SIZE; | ||||||
| pngConverted = await convertSvgToPng(svgText, previewWidth); | ||||||
| const img = new Image(); | ||||||
| img.src = pngConverted; | ||||||
| await new Promise((resolve) => { | ||||||
| img.onload = () => { | ||||||
| dimensions = { width: img.width, height: img.height }; | ||||||
| resolve(null); | ||||||
| }; | ||||||
| }); | ||||||
| } else { | ||||||
| dimensions = await getImageDimensions(blob); | ||||||
| } | ||||||
|
|
||||||
| if (!dimensions) { | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| const { width, height } = dimensions; | ||||||
|
|
||||||
| if (previewWidth && previewWidth > MAX_WIDTH) { | ||||||
| previewWidth = MAX_WIDTH; | ||||||
| } | ||||||
|
|
||||||
| // Convert image to base64 for ODT embedding | ||||||
| const arrayBuffer = pngConverted | ||||||
| ? await (await fetch(pngConverted)).arrayBuffer() | ||||||
| : await blob.arrayBuffer(); | ||||||
| const base64 = btoa( | ||||||
| Array.from(new Uint8Array(arrayBuffer)) | ||||||
| .map((byte) => String.fromCharCode(byte)) | ||||||
| .join(''), | ||||||
| ); | ||||||
|
|
||||||
| const finalWidth = previewWidth || width; | ||||||
| const finalHeight = ((previewWidth || width) / width) * height; | ||||||
|
|
||||||
| // Convert pixels to cm (ODT uses cm for dimensions) | ||||||
| const widthCm = finalWidth / 37.795275591; | ||||||
| const heightCm = finalHeight / 37.795275591; | ||||||
|
|
||||||
| // Create ODT image structure using React.createElement | ||||||
| const frame = React.createElement( | ||||||
| 'text:p', | ||||||
| { | ||||||
| 'text:style-name': | ||||||
| block.props.textAlignment === 'center' | ||||||
| ? 'center' | ||||||
| : block.props.textAlignment === 'right' | ||||||
| ? 'right' | ||||||
| : 'left', | ||||||
| }, | ||||||
| React.createElement( | ||||||
|
Comment on lines
+74
to
+82
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Images seems everytime centered, same with svg btw. |
||||||
| 'draw:frame', | ||||||
| { | ||||||
| 'draw:name': `Image${Date.now()}`, | ||||||
| 'text:anchor-type': 'paragraph', | ||||||
| 'svg:width': `${widthCm}cm`, | ||||||
| 'svg:height': `${heightCm}cm`, | ||||||
| }, | ||||||
| React.createElement( | ||||||
| 'draw:image', | ||||||
| { | ||||||
| 'xlink:type': 'simple', | ||||||
| 'xlink:show': 'embed', | ||||||
| 'xlink:actuate': 'onLoad', | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We get warning in the console:
Suggested change
|
||||||
| }, | ||||||
| React.createElement('office:binary-data', {}, base64), | ||||||
| ), | ||||||
| ), | ||||||
| ); | ||||||
|
|
||||||
| // Add caption if present | ||||||
| if (block.props.caption) { | ||||||
| return [ | ||||||
| frame, | ||||||
| React.createElement( | ||||||
| 'text:p', | ||||||
| { 'text:style-name': 'Caption' }, | ||||||
| block.props.caption, | ||||||
| ), | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| return frame; | ||||||
| } catch (error) { | ||||||
| console.error(`Error processing image for ODT export:`, error); | ||||||
| return null; | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| async function getImageDimensions(blob: Blob) { | ||||||
| if (typeof window !== 'undefined') { | ||||||
| const bmp = await createImageBitmap(blob); | ||||||
| const { width, height } = bmp; | ||||||
| bmp.close(); | ||||||
| return { width, height }; | ||||||
| } | ||||||
| } | ||||||
5 changes: 4 additions & 1 deletion
5
src/frontend/apps/impress/src/features/docs/doc-export/blocks-mapping/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| export * from './calloutDocx'; | ||
| export * from './calloutODT'; | ||
| export * from './calloutPDF'; | ||
| export * from './headingPDF'; | ||
| export * from './imageDocx'; | ||
| export * from './imageODT'; | ||
| export * from './imagePDF'; | ||
| export * from './paragraphPDF'; | ||
| export * from './quoteDocx'; | ||
| export * from './quotePDF'; | ||
| export * from './tablePDF'; | ||
| export * from './uploadLoaderPDF'; | ||
| export * from './uploadLoaderDocx'; | ||
| export * from './uploadLoaderODT'; | ||
| export * from './uploadLoaderPDF'; |
17 changes: 17 additions & 0 deletions
17
src/frontend/apps/impress/src/features/docs/doc-export/blocks-mapping/uploadLoaderODT.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { DocsExporterODT } from '../types'; | ||
|
|
||
| export const blockMappingUploadLoaderODT: DocsExporterODT['mappings']['blockMapping']['uploadLoader'] = | ||
| (block) => { | ||
| // Map uploadLoader to paragraph with information text | ||
| const information = block.props.information || ''; | ||
| const type = block.props.type || 'loading'; | ||
| const prefix = type === 'warning' ? '⚠️ ' : '⏳ '; | ||
|
|
||
| return React.createElement( | ||
| 'text:p', | ||
| { 'text:style-name': 'Text_20_body' }, | ||
| `${prefix}${information}`, | ||
| ); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.