You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-24 23:26:50 +02:00
Desktop: Added support for templates (#1647)
* First pass of adding support for templates * remove default value from template prompt * Add template placeholder text * Add mustache templates with datetime support for new notes * Moved template code to utils, added separate prompt for templates * Add templates to menu and allow for keyboad only use * update template prompt for dark theme * update with laurents suggestions, add refresh button * revert template command, remove new note prompt
This commit is contained in:
committed by
Laurent Cozic
parent
e29fb3eb66
commit
cd5d412c69
@ -546,6 +546,7 @@ class BaseApplication {
|
||||
|
||||
Setting.setConstant('env', initArgs.env);
|
||||
Setting.setConstant('profileDir', profileDir);
|
||||
Setting.setConstant('templateDir', profileDir + '/templates');
|
||||
Setting.setConstant('resourceDirName', resourceDirName);
|
||||
Setting.setConstant('resourceDir', resourceDir);
|
||||
Setting.setConstant('tempDir', tempDir);
|
||||
|
59
ReactNativeClient/lib/TemplateUtils.js
Normal file
59
ReactNativeClient/lib/TemplateUtils.js
Normal file
@ -0,0 +1,59 @@
|
||||
const fs = require('fs-extra');
|
||||
const { shim } = require('lib/shim.js');
|
||||
const { time } = require('lib/time-utils.js');
|
||||
const Mustache = require('mustache');
|
||||
|
||||
const TemplateUtils = {};
|
||||
|
||||
// new template variables can be added here
|
||||
// If there are too many, this should be moved to a new file
|
||||
const view = {
|
||||
date: time.formatMsToLocal(new Date().getTime(), time.dateFormat()),
|
||||
time: time.formatMsToLocal(new Date().getTime(), time.timeFormat()),
|
||||
datetime: time.formatMsToLocal(new Date().getTime()),
|
||||
custom_datetime: () => { return (text, render) => {
|
||||
return render(time.formatMsToLocal(new Date().getTime(), text));
|
||||
}},
|
||||
}
|
||||
|
||||
// Mustache escapes strings (including /) with the html code by default
|
||||
// This isn't useful for markdown so it's disabled
|
||||
Mustache.escape = (text) => { return text; }
|
||||
|
||||
TemplateUtils.render = function(input) {
|
||||
return Mustache.render(input, view);
|
||||
}
|
||||
|
||||
TemplateUtils.loadTemplates = async function(filePath) {
|
||||
let templates = [];
|
||||
let files = [];
|
||||
|
||||
if (await shim.fsDriver().exists(filePath)) {
|
||||
try {
|
||||
files = await shim.fsDriver().readDirStats(filePath);
|
||||
} catch (error) {
|
||||
let msg = error.message ? error.message : '';
|
||||
msg = 'Could not read template names from ' + filePath + '\n' + msg;
|
||||
error.message = msg;
|
||||
throw error;
|
||||
}
|
||||
|
||||
files.forEach(async (file) => {
|
||||
if (file.path.endsWith('.md')) {
|
||||
try {
|
||||
let fileString = await shim.fsDriver().readFile(filePath + '/' + file.path, 'utf-8');
|
||||
templates.push({label: file.path, value: fileString});
|
||||
} catch (error) {
|
||||
let msg = error.message ? error.message : '';
|
||||
msg = 'Could not load template ' + file.path + '\n' + msg;
|
||||
error.message = msg;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return templates;
|
||||
}
|
||||
|
||||
module.exports = TemplateUtils;
|
@ -648,6 +648,7 @@ Setting.constants_ = {
|
||||
resourceDirName: '',
|
||||
resourceDir: '',
|
||||
profileDir: '',
|
||||
templateDir: '',
|
||||
tempDir: '',
|
||||
openDevTools: false,
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ const defaultState = {
|
||||
hasDisabledSyncItems: false,
|
||||
newNote: null,
|
||||
customCss: '',
|
||||
templates: [],
|
||||
collapsedFolderIds: [],
|
||||
clipperServer: {
|
||||
startState: 'idle',
|
||||
@ -713,6 +714,12 @@ const reducer = (state = defaultState, action) => {
|
||||
newState = Object.assign({}, state);
|
||||
newState.customCss = action.css;
|
||||
break;
|
||||
|
||||
case 'TEMPLATE_UPDATE_ALL':
|
||||
|
||||
newState = Object.assign({}, state);
|
||||
newState.templates = action.templates;
|
||||
break;
|
||||
|
||||
case 'SET_NOTE_TAGS':
|
||||
newState = Object.assign({}, state);
|
||||
|
Reference in New Issue
Block a user