introduced NoteFull entity, fixes

This commit is contained in:
azivner 2018-03-25 23:25:17 -04:00
parent cdf94181d2
commit 35998058ce
15 changed files with 184 additions and 165 deletions

View file

@ -10,10 +10,10 @@
<collation id="4" parent="1" name="NOCASE"/>
<collation id="5" parent="1" name="RTRIM"/>
<table id="6" parent="2" name="api_tokens"/>
<table id="7" parent="2" name="attributes"/>
<table id="8" parent="2" name="branches"/>
<table id="9" parent="2" name="event_log"/>
<table id="10" parent="2" name="images"/>
<table id="7" parent="2" name="branches"/>
<table id="8" parent="2" name="event_log"/>
<table id="9" parent="2" name="images"/>
<table id="10" parent="2" name="labels"/>
<table id="11" parent="2" name="note_images"/>
<table id="12" parent="2" name="note_revisions"/>
<table id="13" parent="2" name="notes"/>
@ -59,7 +59,7 @@
<Primary>1</Primary>
<UnderlyingIndexName>sqlite_autoindex_api_tokens_1</UnderlyingIndexName>
</key>
<column id="26" parent="7" name="attributeId">
<column id="26" parent="7" name="branchId">
<Position>1</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
@ -69,198 +69,198 @@
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="28" parent="7" name="name">
<column id="28" parent="7" name="parentNoteId">
<Position>3</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="29" parent="7" name="value">
<Position>4</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
<DefaultExpression>&apos;&apos;</DefaultExpression>
</column>
<column id="30" parent="7" name="position">
<Position>5</Position>
<DataType>INT|0s</DataType>
<NotNull>1</NotNull>
<DefaultExpression>0</DefaultExpression>
</column>
<column id="31" parent="7" name="dateCreated">
<Position>6</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="32" parent="7" name="dateModified">
<Position>7</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="33" parent="7" name="isDeleted">
<Position>8</Position>
<DataType>INT|0s</DataType>
<NotNull>1</NotNull>
</column>
<index id="34" parent="7" name="sqlite_autoindex_attributes_1">
<NameSurrogate>1</NameSurrogate>
<ColNames>attributeId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique>
</index>
<index id="35" parent="7" name="IDX_attributes_noteId">
<ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
</index>
<index id="36" parent="7" name="IDX_attributes_name_value">
<ColNames>name
value</ColNames>
<ColumnCollations>
</ColumnCollations>
</index>
<key id="37" parent="7">
<ColNames>attributeId</ColNames>
<Primary>1</Primary>
<UnderlyingIndexName>sqlite_autoindex_attributes_1</UnderlyingIndexName>
</key>
<column id="38" parent="8" name="branchId">
<Position>1</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="39" parent="8" name="noteId">
<Position>2</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="40" parent="8" name="parentNoteId">
<Position>3</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="41" parent="8" name="notePosition">
<column id="29" parent="7" name="notePosition">
<Position>4</Position>
<DataType>INTEGER|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="42" parent="8" name="prefix">
<column id="30" parent="7" name="prefix">
<Position>5</Position>
<DataType>TEXT|0s</DataType>
</column>
<column id="43" parent="8" name="isExpanded">
<column id="31" parent="7" name="isExpanded">
<Position>6</Position>
<DataType>BOOLEAN|0s</DataType>
</column>
<column id="44" parent="8" name="isDeleted">
<column id="32" parent="7" name="isDeleted">
<Position>7</Position>
<DataType>INTEGER|0s</DataType>
<NotNull>1</NotNull>
<DefaultExpression>0</DefaultExpression>
</column>
<column id="45" parent="8" name="dateModified">
<column id="33" parent="7" name="dateModified">
<Position>8</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<index id="46" parent="8" name="sqlite_autoindex_branches_1">
<index id="34" parent="7" name="sqlite_autoindex_branches_1">
<NameSurrogate>1</NameSurrogate>
<ColNames>branchId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique>
</index>
<index id="47" parent="8" name="IDX_branches_noteId_parentNoteId">
<index id="35" parent="7" name="IDX_branches_noteId_parentNoteId">
<ColNames>noteId
parentNoteId</ColNames>
<ColumnCollations>
</ColumnCollations>
</index>
<index id="48" parent="8" name="IDX_branches_noteId">
<index id="36" parent="7" name="IDX_branches_noteId">
<ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
</index>
<key id="49" parent="8">
<key id="37" parent="7">
<ColNames>branchId</ColNames>
<Primary>1</Primary>
<UnderlyingIndexName>sqlite_autoindex_branches_1</UnderlyingIndexName>
</key>
<column id="50" parent="9" name="id">
<column id="38" parent="8" name="id">
<Position>1</Position>
<DataType>INTEGER|0s</DataType>
<NotNull>1</NotNull>
<SequenceIdentity>1</SequenceIdentity>
</column>
<column id="51" parent="9" name="noteId">
<column id="39" parent="8" name="noteId">
<Position>2</Position>
<DataType>TEXT|0s</DataType>
</column>
<column id="52" parent="9" name="comment">
<column id="40" parent="8" name="comment">
<Position>3</Position>
<DataType>TEXT|0s</DataType>
</column>
<column id="53" parent="9" name="dateAdded">
<column id="41" parent="8" name="dateAdded">
<Position>4</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<key id="54" parent="9">
<key id="42" parent="8">
<ColNames>id</ColNames>
<Primary>1</Primary>
</key>
<foreign-key id="55" parent="9">
<foreign-key id="43" parent="8">
<ColNames>noteId</ColNames>
<RefTableName>notes</RefTableName>
<RefColNames>noteId</RefColNames>
</foreign-key>
<column id="56" parent="10" name="imageId">
<column id="44" parent="9" name="imageId">
<Position>1</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="57" parent="10" name="format">
<column id="45" parent="9" name="format">
<Position>2</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="58" parent="10" name="checksum">
<column id="46" parent="9" name="checksum">
<Position>3</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="59" parent="10" name="name">
<column id="47" parent="9" name="name">
<Position>4</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="60" parent="10" name="data">
<column id="48" parent="9" name="data">
<Position>5</Position>
<DataType>BLOB|0s</DataType>
</column>
<column id="61" parent="10" name="isDeleted">
<column id="49" parent="9" name="isDeleted">
<Position>6</Position>
<DataType>INT|0s</DataType>
<NotNull>1</NotNull>
<DefaultExpression>0</DefaultExpression>
</column>
<column id="62" parent="10" name="dateModified">
<column id="50" parent="9" name="dateModified">
<Position>7</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="63" parent="10" name="dateCreated">
<column id="51" parent="9" name="dateCreated">
<Position>8</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<index id="64" parent="10" name="sqlite_autoindex_images_1">
<index id="52" parent="9" name="sqlite_autoindex_images_1">
<NameSurrogate>1</NameSurrogate>
<ColNames>imageId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique>
</index>
<key id="65" parent="10">
<key id="53" parent="9">
<ColNames>imageId</ColNames>
<Primary>1</Primary>
<UnderlyingIndexName>sqlite_autoindex_images_1</UnderlyingIndexName>
</key>
<column id="54" parent="10" name="labelId">
<Position>1</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="55" parent="10" name="noteId">
<Position>2</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="56" parent="10" name="name">
<Position>3</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="57" parent="10" name="value">
<Position>4</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
<DefaultExpression>&apos;&apos;</DefaultExpression>
</column>
<column id="58" parent="10" name="position">
<Position>5</Position>
<DataType>INT|0s</DataType>
<NotNull>1</NotNull>
<DefaultExpression>0</DefaultExpression>
</column>
<column id="59" parent="10" name="dateCreated">
<Position>6</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="60" parent="10" name="dateModified">
<Position>7</Position>
<DataType>TEXT|0s</DataType>
<NotNull>1</NotNull>
</column>
<column id="61" parent="10" name="isDeleted">
<Position>8</Position>
<DataType>INT|0s</DataType>
<NotNull>1</NotNull>
</column>
<index id="62" parent="10" name="sqlite_autoindex_labels_1">
<NameSurrogate>1</NameSurrogate>
<ColNames>labelId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique>
</index>
<index id="63" parent="10" name="IDX_labels_noteId">
<ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
</index>
<index id="64" parent="10" name="IDX_labels_name_value">
<ColNames>name
value</ColNames>
<ColumnCollations>
</ColumnCollations>
</index>
<key id="65" parent="10">
<ColNames>labelId</ColNames>
<Primary>1</Primary>
<UnderlyingIndexName>sqlite_autoindex_labels_1</UnderlyingIndexName>
</key>
<column id="66" parent="11" name="noteImageId">
<Position>1</Position>
<DataType>TEXT|0s</DataType>

View file

@ -20,7 +20,7 @@ class Note extends Entity {
}
isJson() {
return this.type === "code" && this.mime === "application/json";
return this.mime === "application/json";
}
isJavaScript() {

View file

@ -12,7 +12,7 @@ function showDialog() {
height: 500
});
const noteText = noteDetailService.getCurrentNote().detail.content;
const noteText = noteDetailService.getCurrentNote().content;
$noteSource.text(formatHtml(noteText));
}

View file

@ -0,0 +1,15 @@
import NoteShort from './note_short.js';
class NoteFull extends NoteShort {
constructor(treeCache, row) {
super(treeCache, row);
this.content = row.content;
if (this.isJson()) {
this.jsonContent = JSON.parse(this.content);
}
}
}
export default NoteFull;

View file

@ -9,6 +9,10 @@ class NoteShort {
this.hideInAutocomplete = row.hideInAutocomplete;
}
isJson() {
return this.mime === "application/json";
}
async getBranches() {
const branches = [];

View file

@ -164,7 +164,7 @@ $(document).tooltip({
if (notePath !== null) {
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
noteDetailService.loadNote(noteId).then(note => callback(note.detail.content));
noteDetailService.loadNote(noteId).then(note => callback(note.content));
}
},
close: function(event, ui)

View file

@ -7,6 +7,8 @@ import server from './server.js';
import messagingService from "./messaging.js";
import bundleService from "./bundle.js";
import infoService from "./info.js";
import treeCache from "./tree_cache.js";
import NoteFull from "../entities/note_full.js";
const $noteTitle = $("#note-title");
@ -45,7 +47,7 @@ function getCurrentNote() {
}
function getCurrentNoteId() {
return currentNote ? currentNote.detail.noteId : null;
return currentNote ? currentNote.noteId : null;
}
function noteChanged() {
@ -81,13 +83,13 @@ async function saveNoteIfChanged() {
await saveNoteToServer(note);
if (note.detail.isProtected) {
if (note.isProtected) {
protectedSessionHolder.touchProtectedSession();
}
}
function updateNoteFromInputs(note) {
if (note.detail.type === 'text') {
if (note.type === 'text') {
let content = editor.getData();
// if content is only tags/whitespace (typically <p>&nbsp;</p>), then just make it empty
@ -96,32 +98,32 @@ function updateNoteFromInputs(note) {
content = '';
}
note.detail.content = content;
note.content = content;
}
else if (note.detail.type === 'code') {
note.detail.content = codeEditor.getValue();
else if (note.type === 'code') {
note.content = codeEditor.getValue();
}
else if (note.detail.type === 'search') {
note.detail.content = JSON.stringify({
else if (note.type === 'search') {
note.content = JSON.stringify({
searchString: $searchString.val()
});
}
else if (note.detail.type === 'render' || note.detail.type === 'file') {
else if (note.type === 'render' || note.type === 'file') {
// nothing
}
else {
infoService.throwError("Unrecognized type: " + note.detail.type);
infoService.throwError("Unrecognized type: " + note.type);
}
const title = $noteTitle.val();
note.detail.title = title;
note.title = title;
treeService.setNoteTitle(note.detail.noteId, title);
treeService.setNoteTitle(note.noteId, title);
}
async function saveNoteToServer(note) {
await server.put('notes/' + note.detail.noteId, note);
await server.put('notes/' + note.noteId, note);
isNoteChanged = false;
@ -129,7 +131,7 @@ async function saveNoteToServer(note) {
}
function setNoteBackgroundIfProtected(note) {
const isProtected = !!note.detail.isProtected;
const isProtected = !!note.isProtected;
$noteDetailWrapper.toggleClass("protected", isProtected);
$protectButton.toggle(!isProtected);
@ -143,7 +145,7 @@ function newNoteCreated() {
}
async function setContent(content) {
if (currentNote.detail.type === 'text') {
if (currentNote.type === 'text') {
if (!editor) {
await utils.requireLibrary(utils.CKEDITOR);
@ -157,7 +159,7 @@ async function setContent(content) {
$noteDetail.show();
}
else if (currentNote.detail.type === 'code') {
else if (currentNote.type === 'code') {
if (!codeEditor) {
await utils.requireLibrary(utils.CODE_MIRROR);
@ -186,7 +188,7 @@ async function setContent(content) {
// this needs to happen after the element is shown, otherwise the editor won't be refresheds
codeEditor.setValue(content);
const info = CodeMirror.findModeByMIME(currentNote.detail.mime);
const info = CodeMirror.findModeByMIME(currentNote.mime);
if (info) {
codeEditor.setOption("mode", info.mime);
@ -195,7 +197,7 @@ async function setContent(content) {
codeEditor.refresh();
}
else if (currentNote.detail.type === 'search') {
else if (currentNote.type === 'search') {
$noteDetailSearch.show();
try {
@ -223,9 +225,9 @@ async function loadNoteToEditor(noteId) {
$noteIdDisplay.html(noteId);
await protectedSessionService.ensureProtectedSession(currentNote.detail.isProtected, false);
await protectedSessionService.ensureProtectedSession(currentNote.isProtected, false);
if (currentNote.detail.isProtected) {
if (currentNote.isProtected) {
protectedSessionHolder.touchProtectedSession();
}
@ -237,10 +239,10 @@ async function loadNoteToEditor(noteId) {
noteChangeDisabled = true;
$noteTitle.val(currentNote.detail.title);
$noteTitle.val(currentNote.title);
noteTypeService.setNoteType(currentNote.detail.type);
noteTypeService.setNoteMime(currentNote.detail.mime);
noteTypeService.setNoteType(currentNote.type);
noteTypeService.setNoteMime(currentNote.mime);
$noteDetail.hide();
$noteDetailSearch.hide();
@ -248,7 +250,7 @@ async function loadNoteToEditor(noteId) {
$noteDetailRender.html('').hide();
$noteDetailAttachment.hide();
if (currentNote.detail.type === 'render') {
if (currentNote.type === 'render') {
$noteDetailRender.show();
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
@ -257,15 +259,18 @@ async function loadNoteToEditor(noteId) {
bundleService.executeBundle(bundle);
}
else if (currentNote.detail.type === 'file') {
else if (currentNote.type === 'file') {
const labels = await server.get('notes/' + currentNote.noteId + '/labels');
const labelMap = utils.toObject(labels, l => [l.name, l.value]);
$noteDetailAttachment.show();
$attachmentFileName.text(currentNote.labels.original_file_name);
$attachmentFileSize.text(currentNote.labels.file_size + " bytes");
$attachmentFileType.text(currentNote.detail.mime);
$attachmentFileName.text(labelMap.original_file_name);
$attachmentFileSize.text(labelMap.file_size + " bytes");
$attachmentFileType.text(currentNote.mime);
}
else {
await setContent(currentNote.detail.content);
await setContent(currentNote.content);
}
noteChangeDisabled = false;
@ -299,7 +304,9 @@ async function loadLabelList() {
}
async function loadNote(noteId) {
return await server.get('notes/' + noteId);
const row = await server.get('notes/' + noteId);
return new NoteFull(treeCache, row);
}
function getEditor() {
@ -309,24 +316,24 @@ function getEditor() {
function focus() {
const note = getCurrentNote();
if (note.detail.type === 'text') {
if (note.type === 'text') {
$noteDetail.focus();
}
else if (note.detail.type === 'code') {
else if (note.type === 'code') {
codeEditor.focus();
}
else if (note.detail.type === 'render' || note.detail.type === 'file' || note.detail.type === 'search') {
else if (note.type === 'render' || note.type === 'file' || note.type === 'search') {
// do nothing
}
else {
infoService.throwError('Unrecognized type: ' + note.detail.type);
infoService.throwError('Unrecognized type: ' + note.type);
}
}
function getCurrentNoteType() {
const currentNote = getCurrentNote();
return currentNote ? currentNote.detail.type : null;
return currentNote ? currentNote.type : null;
}
async function executeCurrentNote() {
@ -334,13 +341,13 @@ async function executeCurrentNote() {
// make sure note is saved so we load latest changes
await saveNoteIfChanged();
if (currentNote.detail.mime.endsWith("env=frontend")) {
if (currentNote.mime.endsWith("env=frontend")) {
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
bundleService.executeBundle(bundle);
}
if (currentNote.detail.mime.endsWith("env=backend")) {
if (currentNote.mime.endsWith("env=backend")) {
await server.post('script/run/' + getCurrentNoteId());
}

View file

@ -1,6 +1,6 @@
import treeService from './tree.js';
import noteDetail from './note_detail.js';
import utils from './utils.js';
import server from './server.js';
import infoService from "./info.js";
const $executeScriptButton = $("#execute-script-button");
@ -86,7 +86,7 @@ function NoteTypeModel() {
async function save() {
const note = noteDetail.getCurrentNote();
await server.put('notes/' + note.detail.noteId
await server.put('notes/' + note.noteId
+ '/type/' + encodeURIComponent(self.type())
+ '/mime/' + encodeURIComponent(self.mime()));

View file

@ -94,11 +94,11 @@ async function protectNoteAndSendToServer() {
noteDetail.updateNoteFromInputs(note);
note.detail.isProtected = true;
note.isProtected = true;
await noteDetail.saveNoteToServer(note);
treeService.setProtected(note.detail.noteId, note.detail.isProtected);
treeService.setProtected(note.noteId, note.isProtected);
noteDetail.setNoteBackgroundIfProtected(note);
}
@ -110,11 +110,11 @@ async function unprotectNoteAndSendToServer() {
noteDetail.updateNoteFromInputs(note);
note.detail.isProtected = false;
note.isProtected = false;
await noteDetail.saveNoteToServer(note);
treeService.setProtected(note.detail.noteId, note.detail.isProtected);
treeService.setProtected(note.noteId, note.isProtected);
noteDetail.setNoteBackgroundIfProtected(note);
}

View file

@ -1,4 +1,5 @@
import treeService from './tree.js';
import server from './server.js';
const $tree = $("#tree");
const $searchInput = $("input[name='search-text']");
@ -45,6 +46,8 @@ async function doSearch() {
async function saveSearch() {
const {noteId} = await server.post('search/' + encodeURIComponent($searchInput.val()));
resetSearch();
await treeService.reload();
await treeService.activateNode(noteId);

View file

@ -12,6 +12,7 @@ import recentNotesDialog from '../dialogs/recent_notes.js';
import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js';
import treeCache from './tree_cache.js';
import infoService from "./info.js";
import Branch from '../entities/branch.js';
const $tree = $("#tree");
const $parentList = $("#parent-list");
@ -615,25 +616,22 @@ function initFancyTree(branch) {
}
async function loadSearchNote(searchNoteId) {
const note = await server.get('notes/' + searchNoteId);
const json = JSON.parse(note.detail.content);
const noteIds = await server.get('search/' + encodeURIComponent(json.searchString));
const searchNote = await noteDetailService.loadNote(searchNoteId);
const noteIds = await server.get('search/' + encodeURIComponent(searchNote.jsonContent.searchString));
for (const noteId of noteIds) {
const branchId = "virt" + utils.randomString(10);
treeCache.addBranch({
branchId: branchId,
const branch = new Branch(treeCache, {
branchId: "virt" + utils.randomString(10),
noteId: noteId,
parentNoteId: searchNoteId,
prefix: '',
virtual: true
});
treeCache.addBranch(branch);
}
return await prepareBranchInner(await treeCache.getNote(searchNoteId));
return await prepareBranchInner(searchNote);
}
function getTree() {

View file

@ -167,9 +167,9 @@ function toObject(array, fn) {
const obj = {};
for (const item of array) {
const ret = fn(item);
const [key, value] = fn(item);
obj[ret[0]] = ret[1];
obj[key] = value;
}
return obj;

View file

@ -28,7 +28,7 @@
}
async function validatorJavaScript(text, options) {
if (glob.getCurrentNote().detail.mime === 'application/json') {
if (glob.getCurrentNote().mime === 'application/json') {
// eslint doesn't seem to validate pure JSON well
return [];
}

View file

@ -5,7 +5,6 @@ const router = express.Router();
const auth = require('../../services/auth');
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const labels = require('../../services/labels');
const log = require('../../services/log');
const utils = require('../../services/utils');
const protected_session = require('../../services/protected_session');
@ -26,20 +25,12 @@ router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
protected_session.decryptNote(req, detail);
let labelMap = null;
if (detail.type === 'file') {
// no need to transfer attachment payload for this request
// no need to transfer (potentially large) attachment payload for this request
detail.content = null;
// labels contain important attachment metadata - filename and size
labelMap = await labels.getNoteLabelMap(noteId);
}
res.send({
detail: detail,
labels: labelMap
});
res.send(detail);
}));
router.post('/:parentNoteId/children', auth.checkApiAuth, wrap(async (req, res, next) => {

View file

@ -26,7 +26,8 @@ router.post('/:searchString', auth.checkApiAuth, wrap(async (req, res, next) =>
const noteId = await notes.createNote('root', 'Search note', noteContent, {
json: true,
type: 'search'
type: 'search',
mime: "application/json"
});
res.send({ noteId });