-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
312 lines (263 loc) · 8.83 KB
/
Copy pathutils.ts
File metadata and controls
312 lines (263 loc) · 8.83 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { ItemMap, WorkflowyState, Item } from './types';
export const generateId = (): string => {
return Math.random().toString(36).substr(2, 9);
};
export const INITIAL_ROOT_ID = 'root';
// Get the shared Document ID for Firestore
// Using a static ID ensures all users/browsers engage with the same data
export const getUserDocId = (): string => {
return 'bulletpoints-shared-workspace';
};
// Default seed state for new users
export const getDefaultState = (): WorkflowyState => {
const firstChildId = generateId();
return {
items: {
[INITIAL_ROOT_ID]: {
id: INITIAL_ROOT_ID,
text: 'Home',
children: [firstChildId],
isCompleted: false,
collapsed: false,
isTask: false,
},
[firstChildId]: {
id: firstChildId,
text: 'Welcome to Bulletpoints! Data is now shared across all devices/browsers.',
children: [],
isCompleted: false,
collapsed: false,
isTask: false,
},
},
rootId: INITIAL_ROOT_ID,
};
};
// Helper to find parent of a node (expensive in flat map, but reliable)
export const findParentId = (items: ItemMap, childId: string): string | null => {
for (const id in items) {
if (items[id].children.includes(childId)) {
return id;
}
}
return null;
};
// Check if possibleParent is actually a child of childId (to prevent cycles)
export const isAncestor = (items: ItemMap, possibleAncestorId: string, childId: string): boolean => {
// Direct check
if (possibleAncestorId === childId) return true;
const child = items[childId];
if (!child || child.children.length === 0) return false;
// BFS or DFS to check if possibleAncestorId exists in child's subtree
const stack = [...child.children];
while (stack.length > 0) {
const currentId = stack.pop()!;
if (currentId === possibleAncestorId) return true;
const current = items[currentId];
if (current) {
stack.push(...current.children);
}
}
return false;
};
// Flatten tree to a list of visible IDs for keyboard navigation
export const getVisibleFlatList = (
items: ItemMap,
rootId: string,
list: string[] = []
): string[] => {
const root = items[rootId];
if (!root) return list;
// Don't include the root itself in the navigation list usually,
// but for the recursive function we iterate children
for (const childId of root.children) {
list.push(childId);
const child = items[childId];
if (child && !child.collapsed && child.children.length > 0) {
getVisibleFlatList(items, childId, list);
}
}
return list;
};
export const stripHtml = (html: string): string => {
const tmp = document.createElement('DIV');
tmp.innerHTML = html;
return tmp.textContent || '';
};
export const linkifyHtml = (html: string): string => {
// Create a temporary container to parse HTML
const div = document.createElement('div');
div.innerHTML = html;
const walker = document.createTreeWalker(div, NodeFilter.SHOW_TEXT, null);
let node;
const nodesToReplace: { node: Node, text: string }[] = [];
// First pass: identify text nodes containing URLs
while(node = walker.nextNode()) {
// Skip if already inside an anchor tag
if (node.parentElement?.tagName === 'A') continue;
const text = node.nodeValue || '';
// Regex matches http://, https://, or www.
if (/((https?:\/\/|www\.)[^\s]+)/.test(text)) {
nodesToReplace.push({ node, text });
}
}
if (nodesToReplace.length === 0) return html;
let changed = false;
// Second pass: replace identified nodes
nodesToReplace.forEach(({ node, text }) => {
const fragment = document.createDocumentFragment();
let lastIdx = 0;
const urlRegex = /((?:https?:\/\/|www\.)[^\s]+)/g;
let match;
let localChange = false;
while ((match = urlRegex.exec(text)) !== null) {
localChange = true;
// Append text before URL
if (match.index > lastIdx) {
fragment.appendChild(document.createTextNode(text.substring(lastIdx, match.index)));
}
// Create anchor tag
const url = match[0];
const a = document.createElement('a');
const href = url.startsWith('www.') ? `http://${url}` : url;
a.href = href;
a.textContent = url;
// Updated class list for dark mode compatibility
a.className = "text-blue-500 dark:text-blue-400 underline hover:text-blue-600 dark:hover:text-blue-300 cursor-pointer";
a.target = "_blank";
a.rel = "noopener noreferrer";
a.title = "Cmd+Click to open";
fragment.appendChild(a);
lastIdx = match.index + url.length;
}
if (localChange) {
// Append remaining text
if (lastIdx < text.length) {
fragment.appendChild(document.createTextNode(text.substring(lastIdx)));
}
node.parentNode?.replaceChild(fragment, node);
changed = true;
}
});
return changed ? div.innerHTML : html;
};
export const exportToText = (items: ItemMap, rootId: string): string => {
let output = '';
const processList = (itemIds: string[], depth: number) => {
for (const id of itemIds) {
const item = items[id];
if (!item) continue;
const prefix = '-'.repeat(depth);
const text = stripHtml(item.text);
output += `${prefix}${text}\n`;
if (item.children.length > 0) {
processList(item.children, depth + 1);
}
}
};
const root = items[rootId];
if (root) {
processList(root.children, 0);
}
return output.trim();
};
export const parseImportText = (text: string): WorkflowyState => {
const rootId = INITIAL_ROOT_ID;
const items: ItemMap = {
[rootId]: {
id: rootId,
text: 'Home',
children: [],
isCompleted: false,
collapsed: false,
isTask: false,
}
};
const lines = text.split(/\r?\n/);
// Stack of parent IDs. index 0 = root.
const parentStack = [rootId];
lines.forEach(line => {
// Skip entirely empty lines that might result from trailing newlines
if (!line.trim() && line.length === 0) return;
// Determine depth by counting leading hyphens
const match = line.match(/^(-*)/);
const depth = match ? match[1].length : 0;
// Get content
const content = line.substring(depth);
// Generate Item
const id = generateId();
const newItem: Item = {
id,
text: content,
children: [],
isCompleted: false,
collapsed: false,
fontSize: 'small',
isTask: false,
};
items[id] = newItem;
// Find parent
// If depth is 0, parent is root (stack[0]).
// If depth is 1, parent is last item at depth 0 (stack[1]).
let parentIndex = depth;
if (parentIndex >= parentStack.length) {
parentIndex = parentStack.length - 1;
}
const parentId = parentStack[parentIndex];
if (items[parentId]) {
items[parentId].children.push(id);
}
// Prepare for children of this item (which will be at depth + 1)
parentStack[depth + 1] = id;
parentStack.length = depth + 2;
});
return { items, rootId };
};
// Helper to set cursor at specific character offset within a contentEditable element
export const setCaretPosition = (root: HTMLElement, offset: number) => {
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null);
let node = walker.nextNode();
let currentOffset = 0;
let found = false;
const selection = window.getSelection();
const range = document.createRange();
while (node) {
const length = node.nodeValue?.length || 0;
if (currentOffset + length >= offset) {
range.setStart(node, offset - currentOffset);
range.collapse(true);
selection?.removeAllRanges();
selection?.addRange(range);
found = true;
break;
}
currentOffset += length;
node = walker.nextNode();
}
// If offset is greater than content length, move to end
if (!found) {
range.selectNodeContents(root);
range.collapse(false);
selection?.removeAllRanges();
selection?.addRange(range);
}
};
// Helper to get caret text offset
export const getCaretCharacterOffsetWithin = (element: HTMLElement) => {
let caretOffset = 0;
const doc = element.ownerDocument || document;
const win = doc.defaultView || window;
const sel = win?.getSelection();
if (sel && sel.rangeCount > 0) {
const range = sel.getRangeAt(0);
const preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(element);
preCaretRange.setEnd(range.endContainer, range.endOffset);
caretOffset = preCaretRange.toString().length;
}
return caretOffset;
};
export const isMobile = (): boolean => {
if (typeof window === 'undefined') return false;
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
};