Extract and consolidate indent detection functions

This commit is contained in:
Chris McCormick 2026-01-05 15:26:36 +08:00
parent c3d93998d1
commit 30200ceb7b
2 changed files with 9 additions and 27 deletions

View file

@ -18,10 +18,10 @@
- [ ] Consolidate menu creation (card menus and burger menu)
- [ ] Extract line parsing logic - separate parsing from state management
- [ ] Make createAddForm more generic to handle both columns and cards
- [ ] Extract and consolidate indent detection functions
## Done
- [x] Extract and consolidate indent detection functions
- [x] Reduce IndexedDB boilerplate with generic DB operation wrapper
- [x] Simplify drop indicator logic
- [x] Extract file operations into higher-level abstraction

View file

@ -1121,11 +1121,13 @@ chmod 755 kbtd
}
}
function detectIndentInFile(text) {
const lines = text.split('\n');
function detectIndent(lines, defaultIndent = ' ') {
if (!lines || lines.length === 0) return defaultIndent;
const lineArray = typeof lines === 'string' ? lines.split('\n') : lines;
let minIndent = null;
for (const line of lines) {
for (const line of lineArray) {
if (line.trim() === '') continue;
const match = line.match(/^(\s+)/);
@ -1137,27 +1139,7 @@ chmod 755 kbtd
}
}
return minIndent || ' ';
}
function detectBaseIndent(nestedLines) {
if (nestedLines.length === 0) return '';
let minIndent = null;
for (const line of nestedLines) {
if (line.trim() === '') continue;
const match = line.match(/^(\s+)/);
if (match) {
const indent = match[1];
if (minIndent === null || indent.length < minIndent.length) {
minIndent = indent;
}
}
}
return minIndent || '';
return minIndent || defaultIndent;
}
function stripBaseIndent(nestedLines, baseIndent) {
@ -1734,10 +1716,10 @@ chmod 755 kbtd
const cardDetailModal = createModal('card-detail-modal', async (card) => {
const file = await state.fileHandle.getFile();
const text = await file.text();
const defaultIndent = detectIndentInFile(text);
const defaultIndent = detectIndent(text);
const baseIndent = card.nestedLines.length > 0
? detectBaseIndent(card.nestedLines)
? detectIndent(card.nestedLines, '')
: defaultIndent;
const strippedContent = stripBaseIndent(card.nestedLines, baseIndent);