Skip to content

Commit

Permalink
allow per workspace calendars, fixes #2959
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Jul 31, 2022
1 parent 4c93334 commit 3ebfaec
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const ATTR_HELP = {
"workspace": "marks this note as a workspace which allows easy hoisting",
"workspaceIconClass": "defines box icon CSS class which will be used in tab when hoisted to this note",
"workspaceTabBackgroundColor": "CSS color used in the note tab when hoisted to this note",
"workspaceCalendarRoot": "Defines per-workspace calendar root",
"searchHome": "new search notes will be created as children of this note",
"hoistedSearchHome": "new search notes will be created as children of this note when hoisted to some ancestor of this note",
"inbox": "default inbox location for new notes",
Expand Down
1 change: 1 addition & 0 deletions src/services/builtin_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = [
{ type: 'label', name: 'workspace' },
{ type: 'label', name: 'workspaceIconClass' },
{ type: 'label', name: 'workspaceTabBackgroundColor' },
{ type: 'label', name: 'workspaceCalendarRoot' },
{ type: 'label', name: 'searchHome' },
{ type: 'label', name: 'hoistedInbox' },
{ type: 'label', name: 'hoistedSearchHome' },
Expand Down
25 changes: 20 additions & 5 deletions src/services/date_notes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"use strict";

const noteService = require('./notes');
const becca = require('../becca/becca');
const attributeService = require('./attributes');
const dateUtils = require('./date_utils');
const sql = require('./sql');
const protectedSessionService = require('./protected_session');
const cls = require("./cls");
const searchService = require('../services/search/services/search');
const SearchContext = require('../services/search/search_context');

const CALENDAR_ROOT_LABEL = 'calendarRoot';
const YEAR_LABEL = 'yearNote';
Expand All @@ -26,7 +30,15 @@ function createNote(parentNote, noteTitle) {

/** @returns {Note} */
function getRootCalendarNote() {
let rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL);
let rootNote;

if (cls.getHoistedNoteId() !== 'root') {
rootNote = searchService.findFirstNoteWithQuery('#workspaceCalendarRoot', new SearchContext({ignoreHoistedNote: false}));
}

if (rootNote === null) {
rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL);
}

if (!rootNote) {
sql.transactional(() => {
Expand Down Expand Up @@ -55,7 +67,8 @@ function getYearNote(dateStr, rootNote = null) {

const yearStr = dateStr.trim().substr(0, 4);

let yearNote = attributeService.getNoteWithLabel(YEAR_LABEL, yearStr);
let yearNote = searchService.findFirstNoteWithQuery(`#${YEAR_LABEL}="${yearStr}"`,
new SearchContext({ancestorNoteId: rootNote.noteId}));

if (yearNote) {
return yearNote;
Expand Down Expand Up @@ -95,7 +108,8 @@ function getMonthNote(dateStr, rootNote = null) {
const monthStr = dateStr.substr(0, 7);
const monthNumber = dateStr.substr(5, 2);

let monthNote = attributeService.getNoteWithLabel(MONTH_LABEL, monthStr);
let monthNote = searchService.findFirstNoteWithQuery(`#${MONTH_LABEL}="${monthStr}"`,
new SearchContext({ancestorNoteId: rootNote.noteId}));

if (monthNote) {
return monthNote;
Expand Down Expand Up @@ -137,15 +151,16 @@ function getDayNoteTitle(rootNote, dayNumber, dateObj) {

/** @returns {Note} */
function getDayNote(dateStr) {
const rootNote = getRootCalendarNote();
dateStr = dateStr.trim().substr(0, 10);

let dateNote = attributeService.getNoteWithLabel(DATE_LABEL, dateStr);
let dateNote = searchService.findFirstNoteWithQuery(`#${DATE_LABEL}="${dateStr}"`,
new SearchContext({ancestorNoteId: rootNote.noteId}));

if (dateNote) {
return dateNote;
}

const rootNote = getRootCalendarNote();
const monthNote = getMonthNote(dateStr, rootNote);
const dayNumber = dateStr.substr(8, 2);

Expand Down
12 changes: 12 additions & 0 deletions src/services/search/services/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ function findResultsWithQuery(query, searchContext) {
return findResultsWithExpression(expression, searchContext);
}

/**
* @param {string} query
* @param {SearchContext} searchContext
* @return {Note|null}
*/
function findFirstNoteWithQuery(query, searchContext) {
const searchResults = findResultsWithQuery(query, searchContext);

return searchResults.length > 0 ? becca.notes[searchResults[0].noteId] : null;
}

function searchNotesForAutocomplete(query) {
const searchContext = new SearchContext({
fastSearch: true,
Expand Down Expand Up @@ -279,5 +290,6 @@ function formatAttribute(attr) {
module.exports = {
searchNotesForAutocomplete,
findResultsWithQuery,
findFirstNoteWithQuery,
searchNotes
};

0 comments on commit 3ebfaec

Please sign in to comment.