1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-30 08:26:59 +02:00
joplin/packages/lib/WelcomeUtils.js

81 lines
2.4 KiB
JavaScript
Raw Normal View History

const welcomeAssets = require('./welcomeAssets');
const Note = require('./models/Note').default;
2020-11-05 18:58:23 +02:00
const Setting = require('./models/Setting').default;
const Folder = require('./models/Folder').default;
const Tag = require('./models/Tag').default;
2020-11-05 18:58:23 +02:00
const shim = require('./shim').default;
const uuid = require('./uuid').default;
const { fileExtension, basename } = require('./path-utils');
const { pregQuote } = require('./string-utils');
class WelcomeUtils {
static async createWelcomeItems() {
const output = {
defaultFolderId: null,
};
const folderAssets = welcomeAssets.folders;
const tempDir = Setting.value('resourceDir');
for (let i = 0; i < folderAssets.length; i++) {
const folderAsset = folderAssets[i];
2019-09-19 23:51:18 +02:00
const folder = await Folder.save({ title: `${folderAsset.title} (${Setting.appTypeToLabel(Setting.value('appType'))})` });
if (!output.defaultFolderId) output.defaultFolderId = folder.id;
}
const noteAssets = welcomeAssets.notes;
2019-07-29 15:43:53 +02:00
for (let i = noteAssets.length - 1; i >= 0; i--) {
const noteAsset = noteAssets[i];
let noteBody = noteAsset.body;
for (const resourceUrl in noteAsset.resources) {
if (!noteAsset.resources.hasOwnProperty(resourceUrl)) continue;
const resourceAsset = noteAsset.resources[resourceUrl];
const ext = fileExtension(resourceUrl);
2019-09-19 23:51:18 +02:00
const tempFilePath = `${tempDir}/${uuid.create()}.tmp.${ext}`;
await shim.fsDriver().writeFile(tempFilePath, resourceAsset.body, 'base64');
const resource = await shim.createResourceFromPath(tempFilePath, {
title: basename(resourceUrl),
});
await shim.fsDriver().remove(tempFilePath);
2019-09-19 23:51:18 +02:00
const regex = new RegExp(pregQuote(`(${resourceUrl})`), 'g');
noteBody = noteBody.replace(regex, `(:/${resource.id})`);
}
const note = await Note.save({
parent_id: output.defaultFolderId,
title: noteAsset.title,
body: noteBody,
});
2019-02-03 22:19:17 +02:00
if (noteAsset.tags) await Tag.setNoteTagsByTitles(note.id, noteAsset.tags);
}
return output;
}
static async install(dispatch) {
if (!Setting.value('welcome.enabled')) {
Setting.setValue('welcome.wasBuilt', true);
return;
}
if (!Setting.value('welcome.wasBuilt')) {
const result = await WelcomeUtils.createWelcomeItems();
Setting.setValue('welcome.wasBuilt', true);
dispatch({
type: 'FOLDER_SELECT',
id: result.defaultFolderId,
});
Setting.setValue('activeFolderId', result.defaultFolderId);
}
}
}
2019-07-29 15:43:53 +02:00
module.exports = WelcomeUtils;