appCss/appTheme are now loaded as external CSS files instead of inline styles

This commit is contained in:
zadam 2019-01-27 17:01:37 +01:00
parent 840a0b5f64
commit 2c1580ea65
6 changed files with 34 additions and 28 deletions

View File

@ -37,6 +37,7 @@ import noteTypeService from './services/note_type.js';
import linkService from './services/link.js'; import linkService from './services/link.js';
import noteAutocompleteService from './services/note_autocomplete.js'; import noteAutocompleteService from './services/note_autocomplete.js';
import macInit from './services/mac_init.js'; import macInit from './services/mac_init.js';
import cssLoader from './services/css_loader.js';
// required for CKEditor image upload plugin // required for CKEditor image upload plugin
window.glob.getCurrentNode = treeService.getCurrentNode; window.glob.getCurrentNode = treeService.getCurrentNode;
@ -79,6 +80,10 @@ window.onerror = function (msg, url, lineNo, columnNo, error) {
return false; return false;
}; };
for (const appCssNoteId of window.appCssNoteIds) {
cssLoader.requireCss(`/api/notes/${appCssNoteId}/download`);
}
const wikiBaseUrl = "https://github.com/zadam/trilium/wiki/"; const wikiBaseUrl = "https://github.com/zadam/trilium/wiki/";
$(document).on("click", "button[data-help-page]", e => { $(document).on("click", "button[data-help-page]", e => {

View File

@ -0,0 +1,13 @@
async function requireCss(url) {
const css = Array
.from(document.querySelectorAll('link'))
.map(scr => scr.href);
if (!css.includes(url)) {
$('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', url));
}
}
export default {
requireCss
}

View File

@ -1,3 +1,5 @@
import cssLoader from './css_loader.js';
const CKEDITOR = {"js": ["libraries/ckeditor/ckeditor.js"]}; const CKEDITOR = {"js": ["libraries/ckeditor/ckeditor.js"]};
const CODE_MIRROR = { const CODE_MIRROR = {
@ -34,7 +36,7 @@ const RELATION_MAP = {
async function requireLibrary(library) { async function requireLibrary(library) {
if (library.css) { if (library.css) {
library.css.map(cssUrl => requireCss(cssUrl)); library.css.map(cssUrl => cssLoader.requireCss(cssUrl));
} }
if (library.js) { if (library.js) {
@ -59,16 +61,6 @@ async function requireScript(url) {
await loadedScriptPromises[url]; await loadedScriptPromises[url];
} }
async function requireCss(url) {
const css = Array
.from(document.querySelectorAll('link'))
.map(scr => scr.href);
if (!css.includes(url)) {
$('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', url));
}
}
export default { export default {
requireLibrary, requireLibrary,
CKEDITOR, CKEDITOR,

View File

@ -22,22 +22,13 @@ async function index(req, res) {
sourceId: await sourceIdService.generateSourceId(), sourceId: await sourceIdService.generateSourceId(),
maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync"), maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync"),
instanceName: config.General ? config.General.instanceName : null, instanceName: config.General ? config.General.instanceName : null,
appCss: await getAppCss() appCssNoteIds: await getAppCssNoteIds()
}); });
} }
async function getAppCss() { async function getAppCssNoteIds() {
let css = ''; return (await attributeService.getNotesWithLabels(['appCss', 'appTheme']))
const notes = attributeService.getNotesWithLabel('appCss'); .map(note => note.noteId);
for (const note of await notes) {
css += `/* ${note.noteId} */
${note.content}
`;
}
return css;
} }
module.exports = { module.exports = {

View File

@ -44,6 +44,13 @@ async function getNotesWithLabel(name, value) {
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? ${valueCondition} ORDER BY position`, params); WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? ${valueCondition} ORDER BY position`, params);
} }
async function getNotesWithLabels(names) {
const questionMarks = names.map(() => "?").join(", ");
return await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name IN (${questionMarks}) ORDER BY position`, names);
}
async function getNoteWithLabel(name, value) { async function getNoteWithLabel(name, value) {
const notes = await getNotesWithLabel(name, value); const notes = await getNotesWithLabel(name, value);
@ -86,6 +93,7 @@ async function getAttributeNames(type, nameLike) {
module.exports = { module.exports = {
getNotesWithLabel, getNotesWithLabel,
getNotesWithLabels,
getNoteWithLabel, getNoteWithLabel,
createLabel, createLabel,
createAttribute, createAttribute,

View File

@ -212,6 +212,7 @@
maxSyncIdAtLoad: <%= maxSyncIdAtLoad %>, maxSyncIdAtLoad: <%= maxSyncIdAtLoad %>,
instanceName: '<%= instanceName %>' instanceName: '<%= instanceName %>'
}; };
window.appCssNoteIds = <%- JSON.stringify(appCssNoteIds) %>;
</script> </script>
<!-- Required for correct loading of scripts in Electron --> <!-- Required for correct loading of scripts in Electron -->
@ -247,9 +248,5 @@
// final form which is pretty ugly. // final form which is pretty ugly.
$("#container").show(); $("#container").show();
</script> </script>
<style type="text/css">
<%= appCss %>
</style>
</body> </body>
</html> </html>