Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/super-editor/src/core/DocxZipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
const extractedFiles = await this.unzip(file);
const files = Object.entries(extractedFiles.files);

const mediaObjects = {};

Check warning on line 40 in packages/super-editor/src/core/DocxZipper.js

View workflow job for this annotation

GitHub Actions / validate

'mediaObjects' is assigned a value but never used. Allowed unused vars must match /^_/u
const validTypes = ['xml', 'rels'];
for (const file of files) {
const [, zipEntry] = file;
Expand All @@ -48,12 +48,15 @@
name: zipEntry.name,
content,
});
} else if (zipEntry.name.startsWith('word/media') && zipEntry.name !== 'word/media/') {
} else if (
(zipEntry.name.startsWith('word/media') && zipEntry.name !== 'word/media/') ||
(zipEntry.name.startsWith('media') && zipEntry.name !== 'media/')
) {
// If we are in node, we need to convert the buffer to base64
if (isNode) {
const buffer = await zipEntry.async('nodebuffer');
const fileBase64 = buffer.toString('base64');
mediaObjects[zipEntry.name] = fileBase64;
this.mediaFiles[zipEntry.name] = fileBase64;
}

// If we are in the browser, we can use the base64 directly
Expand Down Expand Up @@ -215,9 +218,9 @@
zip.file(key, content);
});

Object.keys(media).forEach((name) => {
const binaryData = Buffer.from(media[name], 'base64');
zip.file(`word/media/${name}`, binaryData);
Object.keys(media).forEach((path) => {
const binaryData = Buffer.from(media[path], 'base64');
zip.file(path, binaryData);
});

// Export font files
Expand Down Expand Up @@ -252,8 +255,8 @@
unzippedOriginalDocx.file(key, updatedDocs[key]);
});

Object.keys(media).forEach((name) => {
unzippedOriginalDocx.file(`word/media/${name}`, media[name]);
Object.keys(media).forEach((path) => {
unzippedOriginalDocx.file(path, media[path]);
});

await this.updateContentTypes(unzippedOriginalDocx, media);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,7 @@ class SuperConverter {
const processedData = {};
for (const filePath in media) {
if (typeof media[filePath] !== 'string') continue;
const name = filePath.split('/').pop();
processedData[name] = await getArrayBufferFromUrl(media[filePath], editor.options.isHeadless);
processedData[filePath] = await getArrayBufferFromUrl(media[filePath], editor.options.isHeadless);
}

this.convertedXml.media = {
Expand Down
2 changes: 1 addition & 1 deletion packages/super-editor/src/core/super-converter/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ function translateImageNode(params, imageSize) {

const src = attrs.src || attrs.imageSrc;
const { originalWidth, originalHeight } = getPngDimensions(src);
const imageName = params.node.type === 'image' ? src?.split('word/media/')[1] : attrs.fieldId?.replace('-', '_');
const imageName = params.node.type === 'image' ? src.split('/').pop() : attrs.fieldId?.replace('-', '_');

let size = attrs.size
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ export function handleImageImport(node, currentFileName, params) {
if (!rel) return null;

const { attributes: relAttributes } = rel;
const targetPath = relAttributes['Target'];

const path = `word/${relAttributes['Target']}`;
let path = `word/${targetPath}`;

// Some images may appear out of the word folder
if (targetPath.startsWith('/word') || targetPath.startsWith('/media')) path = targetPath.substring(1);

return {
type: 'image',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '@/style.css';
import '@harbour-enterprises/common/styles/common-styles.css';

import { ref, shallowRef, computed, onMounted } from 'vue';
import { NMessageProvider } from 'naive-ui';
import { SuperEditor } from '@/index.js';
import { getFileObject } from '@harbour-enterprises/common/helpers/get-file-object';
import { DOCX } from '@harbour-enterprises/common';
Expand Down Expand Up @@ -158,7 +159,9 @@ onMounted(async () => {
</div>

<div class="dev-app__content" v-if="currentFile">
<SuperEditor :file-source="currentFile" :options="editorOptions" />
<n-message-provider>
<SuperEditor :file-source="currentFile" :options="editorOptions" />
</n-message-provider>
</div>
</div>
</div>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,37 @@ export const getExportedResult = async (name, comments = []) => {
return result;
};

export const getExportMediaFiles = async (name, comments = []) => {
const buffer = await getTestDataAsBuffer(name);
const [docx, media, mediaFiles, fonts] = await Editor.loadXmlData(buffer, true);

const editor = new Editor({
isHeadless: true,
extensions: getStarterExtensions(),
documentId: 'test-doc',
content: docx,
mode: 'docx',
media,
mediaFiles,
fonts,
annotations: true,
});

const json = editor.getUpdatedJson();
await editor.converter.exportToDocx(
json,
editor.schema,
editor.storage.image.media,
true,
'external',
comments,
editor,
false,
null,
);
return editor.converter.addedMedia;
};

export const getExportedResultForAnnotations = async (isFinalDoc) => {
const buffer = await getTestDataAsBuffer('annotations_import.docx');
const [docx, media, mediaFiles, fonts] = await Editor.loadXmlData(buffer, true);
Expand Down
16 changes: 15 additions & 1 deletion packages/super-editor/src/tests/export/imageNodeExporter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getExportedResult } from './export-helpers/index';
import { getExportedResult, getExportMediaFiles } from './export-helpers/index';

describe('ImageNodeExporter', async () => {
window.URL.createObjectURL = vi.fn().mockImplementation((file) => {
Expand Down Expand Up @@ -68,3 +68,17 @@ describe('ImageNodeExporter anchor image', async () => {
expect(anchorNode.elements[5].attributes.wrapText).toBe('bothSides');
});
});

describe('ImageNodeExporter images with absolute path', async () => {
window.URL.createObjectURL = vi.fn().mockImplementation((file) => {
return file.name;
});

const fileName = 'image-out-of-folder.docx';
const result = await getExportMediaFiles(fileName);

it('exports image with absolute path correctly', async () => {
expect(result).toHaveProperty('word/media/image1.jpeg');
expect(result).toHaveProperty('media/image.png');
});
});
27 changes: 27 additions & 0 deletions packages/super-editor/src/tests/import/imageImporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,31 @@ describe('ImageNodeImporter', () => {
expect(anchorData).toHaveProperty('alignH', 'left');
expect(anchorData).toHaveProperty('alignV', 'top');
});

it('imports image with absolute path in Target correctly', async () => {
const dataName = 'image-out-of-folder.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];

const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
console.log(content[6].elements[2]);

const { nodes } = handleParagraphNode({ nodes: [content[0]], docx, nodeListHandler: defaultNodeListHandler() });

let paragraphNode = nodes[0];
let drawingNode = paragraphNode.content[0];
const { attrs } = drawingNode;
expect(attrs.src).toBe('media/image.png');

const { nodes: nodes1 } = handleParagraphNode({
nodes: [content[5]],
docx,
nodeListHandler: defaultNodeListHandler(),
});
paragraphNode = nodes1[0];
drawingNode = paragraphNode.content[1];
expect(drawingNode.attrs.src).toBe('word/media/image1.jpeg');
});
});
Loading