-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjournal.user.js
205 lines (186 loc) · 7.06 KB
/
journal.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// ==UserScript==
// @name Workflowy Journal
// @namespace https://github.com/mivok/workflowy-userscripts
// @version 0.0.2
// @description Quickly create/select a daily journal item
// @author Mark Harrison
// @match https://workflowy.com/*
// @match https://beta.workflowy.com/*
// @updateUrl https://github.com/mivok/workflowy-userscripts/raw/master/journal.user.js
// @downloadUrl https://github.com/mivok/workflowy-userscripts/raw/master/journal.user.js
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
// Utility functions
// Find and return the first item with the given tag
//
// Example: const item = findTaggedItem(WF.rootItem(), '#mytag');
function findTaggedItem(item, tag) {
const myItemTags = WF.getItemTags(item).concat(
WF.getItemNoteTags(item));
const myTags = myItemTags.map(t => t.tag);
if (myTags.includes(tag)) {
// We found a matching item
return item
}
const tagCounts = item.isMainDocumentRoot() ?
getRootDescendantTagCounts() :
item.getTagManager().descendantTagCounts;
const tagList = tagCounts ? tagCounts.getTagList().map(t => t.tag) : [];
if (!tagList.includes(tag)) {
// We don't have a matching tag underneath us
return null
}
for (const child of item.getChildren()) {
const match = findTaggedItem(child, tag);
if (match) {
return match
}
}
// No match
return null
}
// Find and returns all items tagged with a given
//
// Example: const items = findTaggedItems(WF.rootItem(), '#mytag');
function findTaggedItems(item, tag) {
let found = [];
// Check the current item first
const myTags = WF.getItemTags(item).concat(
WF.getItemNoteTags(item)).map(t => t.tag);
if (myTags.includes(tag)) {
// We found a matching item
found.push(item);
}
// Look at tag counts to quickly decide whether to descend into children
const tagCounts = item.isMainDocumentRoot() ?
getRootDescendantTagCounts() :
item.getTagManager().descendantTagCounts;
const tagList = tagCounts ? tagCounts.getTagList().map(t => t.tag) : [];
if (tagList.includes(tag)) {
// The tag counds say that we have some matching items, so go through
// the chilren
for (const child of item.getChildren()) {
found.push(...findTaggedItems(child, tag));
}
}
return found
}
// Find a child item matching the given name, or create it if one isn't found.
//
// Example: const newItem = findOrCreate(myItem, 'Some Title')
function findOrCreateItem(parent, name) {
for (const candidateItem of parent.getChildren()) {
if (candidateItem.getName() == name) {
return candidateItem;
}
}
const newItem = WF.createItem(parent);
WF.setItemName(newItem, name);
return newItem;
}
// Wait until the document has finished loading and the workflowy header
// is available
//
// Example: onHeaderAvailable((header) => { ... });
function onHeaderAvailable(callback) {
setTimeout(() => {
const header = document.querySelector('.header');
if (header) {
callback(header);
} else {
// Try again until header is available
onHeaderAvailable(callback)
}
}, 500);
}
// Add a button to the left of the gear menu
//
// Example: addButton('X', doTheThing);
function addButton(icon, buttonCallback) {
onHeaderAvailable((header) => {
const button = document.createElement('div');
button.innerHTML = icon
// Style/Hover behavior like existing menu buttons
button.className = 'extension_button';
const buttonCss = `
.extension_button:hover {
background-color: rgb(236, 238, 240);
}
.extension_button {
font-weight: bold;
border-radius: 18px;
width: 36px;
height: 36px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
`;
// Add the style element, if it's not already there
if (!document.querySelector("#extension_button_style")) {
const style = document.createElement('style');
style.id = 'extension_button_style';
style.appendChild(document.createTextNode(buttonCss));
header.insertBefore(style, header.querySelector('.gearMenu'));
}
button.addEventListener('click', buttonCallback);
header.insertBefore(button, header.querySelector('.gearMenu'));
});
}
// Get today's date in YYYY-MM-DD format
//
// Example: const todayStr = getTodayString(); // YYYY-MM-DD
function getTodayString() {
const todayDate = new Date();
const offset = todayDate.getTimezoneOffset();
const offsetDate = new Date(todayDate.getTime() - (offset*60*1000));
return offsetDate.toISOString().split('T')[0];
}
// Set multiple styles at once
function setStyle(elem, styles) {
for (let style in styles) {
elem.style[style] = styles[style]
}
}
// Add a style element (to add actual CSS from JS)
function addStylesheet(stylesheet) {
const element = document.createElement('style')
element.innerHTML = stylesheet;
document.head.insertBefore(element, null);
}
// Main script
// The tag to look for as the root of the journal
const journalItemTag = '#journalroot';
function createJournalItem() {
const journalItem = findTaggedItem(WF.rootItem(), journalItemTag);
if (!journalItem) {
WF.showMessage(`Unable to find journal root item`, true);
return;
}
const todayStr = getTodayString(); // YYYY-MM-DD
const monthStr = todayStr.slice(0,7); // YYYY-MM
// Create child item
WF.editGroup(function() {
// Find the month item
const monthItem = findOrCreateItem(journalItem, monthStr);
const todayItem = findOrCreateItem(monthItem, todayStr);
// Select the item and put the cursor in the right place
WF.zoomTo(todayItem);
WF.editItemName(todayItem);
});
}
// Pencil symbol
addButton('✎', createJournalItem);
// Add keyboard shortcut
document.addEventListener("keydown", function (event) {
if (!event.altKey && event.shiftKey &&
event.ctrlKey && !event.metaKey && event.key === "J") {
createJournalItem();
event.preventDefault();
}
});
})();