Refactoring markdown updating.

This commit is contained in:
Chris McCormick 2026-01-01 15:03:46 +08:00
parent d3629d8b30
commit d12b1a9d56

View file

@ -687,21 +687,24 @@ chmod 755 kbtd
} }
} }
async function updateLineInFile(lineNum, newText) { async function modifyFileLines(fn) {
console.log('updateLineInFile:', lineNum, newText);
const file = await state.fileHandle.getFile(); const file = await state.fileHandle.getFile();
const text = await file.text(); const text = await file.text();
const lines = text.split('\n'); const lines = text.split('\n');
lines[lineNum] = newText; fn(lines);
await writeFile(lines.join('\n')); await writeFile(lines.join('\n'));
} }
async function updateLineInFile(lineNum, newText) {
console.log('updateLineInFile:', lineNum, newText);
await modifyFileLines(lines => {
lines[lineNum] = newText;
});
}
async function moveLineInFile(fromLine, toLine, targetColumnIndex, insertPosition) { async function moveLineInFile(fromLine, toLine, targetColumnIndex, insertPosition) {
console.log('moveLineInFile: from line', fromLine, 'to line', toLine, 'targetColumnIndex:', targetColumnIndex, 'insertPosition:', insertPosition); console.log('moveLineInFile: from line', fromLine, 'to line', toLine, 'targetColumnIndex:', targetColumnIndex, 'insertPosition:', insertPosition);
const file = await state.fileHandle.getFile(); await modifyFileLines(lines => {
const text = await file.text();
const lines = text.split('\n');
const section = state.sections[state.currentSectionIndex]; const section = state.sections[state.currentSectionIndex];
const targetColumn = section.columns[targetColumnIndex]; const targetColumn = section.columns[targetColumnIndex];
const targetColumnIsEmpty = targetColumn.cards.length === 0; const targetColumnIsEmpty = targetColumn.cards.length === 0;
@ -774,23 +777,22 @@ chmod 755 kbtd
for (let i = Math.max(0, actualInsertLine - 2); i <= Math.min(lines.length - 1, actualInsertLine + 2); i++) { for (let i = Math.max(0, actualInsertLine - 2); i <= Math.min(lines.length - 1, actualInsertLine + 2); i++) {
console.log(` line ${i}: "${lines[i]}"`); console.log(` line ${i}: "${lines[i]}"`);
} }
});
await writeFile(lines.join('\n'));
} }
async function createProject(projectName) { async function createProject(projectName) {
console.log('Creating project:', projectName); console.log('Creating project:', projectName);
const file = await state.fileHandle.getFile(); await modifyFileLines(lines => {
const text = await file.text(); const text = lines.join('\n');
let newText; let newText;
if (text.trim() === '') { if (text.trim() === '') {
newText = `# ${projectName}\n\n## TODO\n\n## In Progress\n\n## Done\n`; newText = `# ${projectName}\n\n## TODO\n\n## In Progress\n\n## Done\n`;
} else { } else {
newText = text + (text.endsWith('\n') ? '' : '\n') + `\n# ${projectName}\n\n## TODO\n\n## In Progress\n\n## Done\n`; newText = text + (text.endsWith('\n') ? '' : '\n') + `\n# ${projectName}\n\n## TODO\n\n## In Progress\n\n## Done\n`;
} }
lines.length = 0;
await writeFile(newText); lines.push(...newText.split('\n'));
});
const newSectionIndex = state.sections.findIndex(s => s.name === projectName); const newSectionIndex = state.sections.findIndex(s => s.name === projectName);
if (newSectionIndex !== -1) { if (newSectionIndex !== -1) {
@ -800,10 +802,7 @@ chmod 755 kbtd
async function createColumn(columnName) { async function createColumn(columnName) {
console.log('Creating column:', columnName); console.log('Creating column:', columnName);
const file = await state.fileHandle.getFile(); await modifyFileLines(lines => {
const text = await file.text();
const lines = text.split('\n');
const section = state.sections[state.currentSectionIndex]; const section = state.sections[state.currentSectionIndex];
let insertLine; let insertLine;
@ -834,16 +833,12 @@ chmod 755 kbtd
if (insertLine + 1 >= lines.length || lines[insertLine + 1].trim() !== '') { if (insertLine + 1 >= lines.length || lines[insertLine + 1].trim() !== '') {
lines.splice(insertLine + 1, 0, ''); lines.splice(insertLine + 1, 0, '');
} }
});
await writeFile(lines.join('\n'));
} }
async function createCard(columnIndex, cardText) { async function createCard(columnIndex, cardText) {
console.log('Creating card in column', columnIndex, ':', cardText); console.log('Creating card in column', columnIndex, ':', cardText);
const file = await state.fileHandle.getFile(); await modifyFileLines(lines => {
const text = await file.text();
const lines = text.split('\n');
const section = state.sections[state.currentSectionIndex]; const section = state.sections[state.currentSectionIndex];
const column = section.columns[columnIndex]; const column = section.columns[columnIndex];
@ -871,8 +866,7 @@ chmod 755 kbtd
} }
console.log('Inserted new card at line', insertLine); console.log('Inserted new card at line', insertLine);
});
await writeFile(lines.join('\n'));
} }
async function loadFile() { async function loadFile() {