1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Desktop: Fixes #2968: Trying to resource path issue in WYSIWYG editor

This commit is contained in:
Laurent Cozic 2020-04-13 22:55:24 +00:00
parent 6164e2d8eb
commit 2050889590
4 changed files with 66 additions and 4 deletions

View File

@ -6,6 +6,7 @@ const { time } = require('lib/time-utils.js');
const { sortedIds, createNTestNotes, asyncTest, fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
const Folder = require('lib/models/Folder.js');
const Note = require('lib/models/Note.js');
const Setting = require('lib/models/Setting.js');
const BaseModel = require('lib/BaseModel.js');
const ArrayUtils = require('lib/ArrayUtils.js');
const { shim } = require('lib/shim');
@ -216,4 +217,54 @@ describe('models_Note', function() {
const hasThrown = await checkThrowAsync(async () => await Folder.copyToFolder(note1.id, folder2.id));
expect(hasThrown).toBe(true);
}));
it('should convert resource paths from internal to external paths', asyncTest(async () => {
const resourceDirName = Setting.value('resourceDirName');
const resourceDir = Setting.value('resourceDir');
const r1 = await shim.createResourceFromPath(`${__dirname}/../tests/support/photo.jpg`);
const r2 = await shim.createResourceFromPath(`${__dirname}/../tests/support/photo.jpg`);
const testCases = [
[
false,
'',
'',
],
[
true,
'',
'',
],
[
false,
`![](:/${r1.id})`,
`![](${resourceDirName}/${r1.id}.jpg)`,
],
[
false,
`![](:/${r1.id}) ![](:/${r1.id}) ![](:/${r2.id})`,
`![](${resourceDirName}/${r1.id}.jpg) ![](${resourceDirName}/${r1.id}.jpg) ![](${resourceDirName}/${r2.id}.jpg)`,
],
[
true,
`![](:/${r1.id})`,
`![](file://${resourceDir}/${r1.id}.jpg)`,
],
[
true,
`![](:/${r1.id}) ![](:/${r1.id}) ![](:/${r2.id})`,
`![](file://${resourceDir}/${r1.id}.jpg) ![](file://${resourceDir}/${r1.id}.jpg) ![](file://${resourceDir}/${r2.id}.jpg)`,
],
];
for (const testCase of testCases) {
const [useAbsolutePaths, input, expected] = testCase;
const internalToExternal = await Note.replaceResourceInternalToExternalLinks(input, { useAbsolutePaths });
expect(expected).toBe(internalToExternal);
const externalToInternal = await Note.replaceResourceExternalToInternalLinks(internalToExternal, { useAbsolutePaths });
expect(externalToInternal).toBe(input);
}
}));
});

View File

@ -143,6 +143,7 @@ async function switchClient(id) {
Resource.encryptionService_ = encryptionServices_[id];
BaseItem.revisionService_ = revisionServices_[id];
Setting.setConstant('resourceDirName', resourceDirName(id));
Setting.setConstant('resourceDir', resourceDir(id));
await Setting.load();
@ -213,9 +214,14 @@ async function setupDatabase(id = null) {
if (!Setting.value('clientId')) Setting.setValue('clientId', uuid.create());
}
function resourceDirName(id = null) {
if (id === null) id = currentClient_;
return `resources-${id}`;
}
function resourceDir(id = null) {
if (id === null) id = currentClient_;
return `${__dirname}/data/resources-${id}`;
return `${__dirname}/data/${resourceDirName(id)}`;
}
async function setupDatabaseAndSynchronizer(id = null) {

View File

@ -445,6 +445,7 @@ const TinyMCE = (props:TinyMCEProps, ref:any) => {
noneditable_noneditable_class: 'joplin-editable', // Can be a regex too
valid_elements: '*[*]', // We already filter in sanitize_html
menubar: false,
relative_urls: false,
branding: false,
target_list: false,
table_resize_bars: false,

View File

@ -162,10 +162,12 @@ class Note extends BaseItem {
const id = resourceIds[i];
const resource = await Resource.load(id);
if (!resource) continue;
const resourcePath = options.useAbsolutePaths ? Resource.fullPath(resource) : Resource.relativePath(resource);
const resourcePath = options.useAbsolutePaths ? `file://${Resource.fullPath(resource)}` : Resource.relativePath(resource);
body = body.replace(new RegExp(`:/${id}`, 'gi'), resourcePath);
}
this.logger().info('replaceResourceInternalToExternalLinks result', body);
return body;
}
@ -176,8 +178,8 @@ class Note extends BaseItem {
const pathsToTry = [];
if (options.useAbsolutePaths) {
pathsToTry.push(Setting.value('resourceDir'));
pathsToTry.push(shim.pathRelativeToCwd(Setting.value('resourceDir')));
pathsToTry.push(`file://${Setting.value('resourceDir')}`);
pathsToTry.push(`file://${shim.pathRelativeToCwd(Setting.value('resourceDir'))}`);
} else {
pathsToTry.push(Resource.baseRelativeDirectoryPath());
}
@ -193,6 +195,8 @@ class Note extends BaseItem {
});
}
this.logger().info('replaceResourceExternalToInternalLinks result', body);
return body;
}