Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ cache
# vite
vite.config.ts.timestamp*
config.ts.timestamp*
dist
dist
public/fonts/__fallback__/
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getAllQueryString } from 'ranuts/utils';
import { initEmbedApi } from './lib/embed-api';
import { initEvents, setEventUICallbacks } from './lib/events';
import { onCreateNew, openDocumentFromUrl, setUICallbacks } from './lib/document';
import {
Expand All @@ -25,6 +26,7 @@ declare global {

// Initialize events
initEvents();
initEmbedApi();

// Set up UI callbacks to avoid circular dependency
setUICallbacks({
Expand Down
18 changes: 14 additions & 4 deletions lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getExtensions } from 'ranuts/utils';
import { g_sEmpty_bin } from './empty_bin';
import { t } from './i18n';
import { X2TConverter } from './document-converter';
import { createEditorInstance, loadEditorApi, setConverterCallback } from './onlyoffice-editor';
import { createEditorInstance, loadEditorApi, setConverterCallbacks } from './onlyoffice-editor';
import { getDocumentType } from './document-utils';
import type { BinConversionResult, ConversionResult, EmscriptenModule } from './document-types';

Expand All @@ -28,6 +28,11 @@ const x2tConverter = new X2TConverter();
export const loadScript = (): Promise<void> => x2tConverter.loadScript();
export const initX2T = (): Promise<EmscriptenModule> => x2tConverter.initialize();
export const convertDocument = (file: File): Promise<ConversionResult> => x2tConverter.convertDocument(file);
export const convertBinToDocument = (
bin: Uint8Array,
fileName: string,
targetExt?: string,
): Promise<BinConversionResult> => x2tConverter.convertBinToDocument(bin, fileName, targetExt);
export const convertBinToDocumentAndDownload = (
bin: Uint8Array,
fileName: string,
Expand All @@ -38,17 +43,21 @@ export const convertBinToDocumentAndDownload = (
export { createEditorInstance, loadEditorApi };

// Set up converter callback for editor
setConverterCallback(convertBinToDocumentAndDownload);
setConverterCallbacks({
convert: convertBinToDocument,
convertAndDownload: convertBinToDocumentAndDownload,
});

// Merged file operation method
export async function handleDocumentOperation(options: {
isNew: boolean;
fileName: string;
file?: File;
readonly?: boolean;
}): Promise<void> {
try {
const { isNew, fileName, file } = options;
const fileType = getExtensions(file?.type || '')[0] || fileName.split('.').pop() || '';
const { isNew, fileName, file, readonly = false } = options;
const fileType = fileName.split('.').pop() || getExtensions(file?.type || '')[0] || '';
const _docType = getDocumentType(fileType);

// Get document content
Expand Down Expand Up @@ -77,6 +86,7 @@ export async function handleDocumentOperation(options: {
fileType,
binData: documentData.bin,
media: documentData.media,
readonly,
});
} catch (error: any) {
console.error(`${t('documentOperationFailed')}`, error);
Expand Down
29 changes: 19 additions & 10 deletions lib/document-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class X2TConverter {
await this.initialize();

const fileName = file.name;
const fileExt = getExtensions(file?.type)[0] || fileName.split('.').pop() || '';
const fileExt = fileName.split('.').pop() || getExtensions(file?.type)[0] || '';
const documentType = this.getDocumentType(fileExt);

try {
Expand Down Expand Up @@ -436,9 +436,9 @@ export class X2TConverter {
}

/**
* Convert bin format to specified format and download
* Convert bin format to specified document format.
*/
async convertBinToDocumentAndDownload(
async convertBinToDocument(
bin: Uint8Array,
originalFileName: string,
targetExt = 'DOCX',
Expand Down Expand Up @@ -483,9 +483,6 @@ export class X2TConverter {
csvArray.set(csvBOM, 0);
csvArray.set(csvTextBytes, csvBOM.length);

// Save CSV file
await this.saveWithFileSystemAPI(csvArray, outputFileName);

return {
fileName: outputFileName,
data: csvArray,
Expand Down Expand Up @@ -519,10 +516,6 @@ export class X2TConverter {
// Ensure result is Uint8Array type
const resultArray = result instanceof Uint8Array ? result : new Uint8Array(result as ArrayBuffer);

// Download file
// TODO: Improve print functionality
await this.saveWithFileSystemAPI(resultArray, outputFileName);

return {
fileName: outputFileName,
data: result,
Expand All @@ -532,6 +525,22 @@ export class X2TConverter {
}
}

/**
* Convert bin format to specified format and save it locally.
*/
async convertBinToDocumentAndDownload(
bin: Uint8Array,
originalFileName: string,
targetExt = 'DOCX',
): Promise<BinConversionResult> {
const result = await this.convertBinToDocument(bin, originalFileName, targetExt);
const data = result.data instanceof Uint8Array ? result.data : new Uint8Array(result.data as ArrayBuffer);

// TODO: Improve print functionality
await this.saveWithFileSystemAPI(data, result.fileName);
return result;
}

/**
* Download file
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/document-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ declare global {
buf?: ArrayBuffer;
success?: boolean;
error?: string;
enabled?: boolean;
message?: string;
};
}) => void;
downloadAs?: (data?: string) => void;
destroyEditor: () => void;
};
}
Expand Down
18 changes: 15 additions & 3 deletions lib/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ export const onOpenDocument = (): void => {
fileInput.click();
};

export const openDocumentFromUrl = async (url: string, fileName?: string): Promise<void> => {
export const openDocumentFromUrl = async (
url: string,
fileName?: string,
options?: {
readonly?: boolean;
fetchOptions?: RequestInit;
},
): Promise<void> => {
const { removeLoading } = showLoading();
try {
if (hideControlPanelFn) {
Expand All @@ -126,7 +133,7 @@ export const openDocumentFromUrl = async (url: string, fileName?: string): Promi
// Fetch the file from URL
console.log('Fetching document from URL:', url);
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const response = await fetch(url);
const response = await fetch(url, options?.fetchOptions);

if (!response.ok) {
throw new Error(`Failed to fetch document: ${response.status} ${response.statusText}`);
Expand Down Expand Up @@ -172,7 +179,12 @@ export const openDocumentFromUrl = async (url: string, fileName?: string): Promi
// Initialize and open document
await initX2T();
const { fileName: docFileName, file: fileBlob } = getDocmentObj();
await handleDocumentOperation({ file: fileBlob, fileName: docFileName, isNew: !fileBlob });
await handleDocumentOperation({
file: fileBlob,
fileName: docFileName,
isNew: !fileBlob,
readonly: options?.readonly,
});

// Show menu guide after document is loaded
if (showMenuGuideFn) {
Expand Down
Loading