2023-07-18 15:58:06 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2024-04-08 18:57:01 +02:00
|
|
|
import { FunctionComponent } from 'react';
|
2023-07-18 15:58:06 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import { join } from 'path';
|
2024-04-08 18:57:01 +02:00
|
|
|
import exportAllFolders from './utils/exportAllFolders';
|
2023-07-18 15:58:06 +02:00
|
|
|
import { ExportProgressState } from '@joplin/lib/services/interop/types';
|
|
|
|
import { ConfigScreenStyles } from '../configScreenStyles';
|
2024-04-08 18:57:01 +02:00
|
|
|
import makeImportExportCacheDirectory from './utils/makeImportExportCacheDirectory';
|
|
|
|
import TaskButton, { OnProgressCallback, SetAfterCompleteListenerCallback, TaskStatus } from './TaskButton';
|
2024-08-02 15:51:49 +02:00
|
|
|
import shareFile from '../../../../utils/shareFile';
|
2023-07-18 15:58:06 +02:00
|
|
|
|
|
|
|
const logger = Logger.create('NoteExportButton');
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
styles: ConfigScreenStyles;
|
|
|
|
}
|
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
export const exportButtonDefaultTitle = () => _('Export all notes as JEX');
|
2023-11-16 14:17:03 +02:00
|
|
|
export const exportButtonDescription = () => _('Share a copy of all notes in a file format that can be imported by Joplin on a computer.');
|
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
const getTitle = (taskStatus: TaskStatus) => {
|
|
|
|
if (taskStatus === TaskStatus.InProgress) {
|
|
|
|
return _('Exporting...');
|
|
|
|
} else {
|
|
|
|
return exportButtonDefaultTitle();
|
|
|
|
}
|
|
|
|
};
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
const runExportTask = async (
|
|
|
|
onProgress: OnProgressCallback,
|
|
|
|
setAfterCompleteListener: SetAfterCompleteListenerCallback,
|
|
|
|
) => {
|
|
|
|
const exportTargetPath = join(await makeImportExportCacheDirectory(), 'jex-export.jex');
|
|
|
|
logger.info(`Exporting all folders to path ${exportTargetPath}`);
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
setAfterCompleteListener(async (success: boolean) => {
|
|
|
|
if (success) {
|
2024-08-02 15:51:49 +02:00
|
|
|
await shareFile(exportTargetPath, 'application/jex');
|
2023-07-18 15:58:06 +02:00
|
|
|
}
|
2024-04-08 18:57:01 +02:00
|
|
|
await shim.fsDriver().remove(exportTargetPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initially, undetermined progress
|
|
|
|
onProgress(undefined);
|
|
|
|
|
|
|
|
const status = await exportAllFolders(exportTargetPath, (status, progress) => {
|
|
|
|
if (progress !== null) {
|
|
|
|
onProgress(progress);
|
|
|
|
} else if (status === ExportProgressState.Closing || status === ExportProgressState.QueuingItems) {
|
|
|
|
// We don't have a numeric progress value and the closing/queuing state may take a while.
|
|
|
|
// Set a special progress value:
|
|
|
|
onProgress(undefined);
|
|
|
|
}
|
|
|
|
});
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
onProgress(1);
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
logger.info('Export complete');
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
return { warnings: status.warnings, success: true };
|
|
|
|
};
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2024-04-08 18:57:01 +02:00
|
|
|
const NoteExportButton: FunctionComponent<Props> = props => {
|
|
|
|
return (
|
|
|
|
<TaskButton
|
|
|
|
taskName={exportButtonDefaultTitle()}
|
|
|
|
buttonLabel={getTitle}
|
|
|
|
finishedLabel={_('Exported successfully!')}
|
|
|
|
description={exportButtonDescription()}
|
|
|
|
styles={props.styles}
|
|
|
|
onRunTask={runExportTask}
|
|
|
|
/>
|
|
|
|
);
|
2023-07-18 15:58:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default NoteExportButton;
|