Refactoring markdown updating.
This commit is contained in:
parent
d3629d8b30
commit
d12b1a9d56
1 changed files with 135 additions and 141 deletions
46
index.html
46
index.html
|
|
@ -687,21 +687,24 @@ chmod 755 kbtd
|
|||
}
|
||||
}
|
||||
|
||||
async function updateLineInFile(lineNum, newText) {
|
||||
console.log('updateLineInFile:', lineNum, newText);
|
||||
async function modifyFileLines(fn) {
|
||||
const file = await state.fileHandle.getFile();
|
||||
const text = await file.text();
|
||||
const lines = text.split('\n');
|
||||
lines[lineNum] = newText;
|
||||
fn(lines);
|
||||
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) {
|
||||
console.log('moveLineInFile: from line', fromLine, 'to line', toLine, 'targetColumnIndex:', targetColumnIndex, 'insertPosition:', insertPosition);
|
||||
const file = await state.fileHandle.getFile();
|
||||
const text = await file.text();
|
||||
const lines = text.split('\n');
|
||||
|
||||
await modifyFileLines(lines => {
|
||||
const section = state.sections[state.currentSectionIndex];
|
||||
const targetColumn = section.columns[targetColumnIndex];
|
||||
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++) {
|
||||
console.log(` line ${i}: "${lines[i]}"`);
|
||||
}
|
||||
|
||||
await writeFile(lines.join('\n'));
|
||||
});
|
||||
}
|
||||
|
||||
async function createProject(projectName) {
|
||||
console.log('Creating project:', projectName);
|
||||
const file = await state.fileHandle.getFile();
|
||||
const text = await file.text();
|
||||
|
||||
await modifyFileLines(lines => {
|
||||
const text = lines.join('\n');
|
||||
let newText;
|
||||
if (text.trim() === '') {
|
||||
newText = `# ${projectName}\n\n## TODO\n\n## In Progress\n\n## Done\n`;
|
||||
} else {
|
||||
newText = text + (text.endsWith('\n') ? '' : '\n') + `\n# ${projectName}\n\n## TODO\n\n## In Progress\n\n## Done\n`;
|
||||
}
|
||||
|
||||
await writeFile(newText);
|
||||
lines.length = 0;
|
||||
lines.push(...newText.split('\n'));
|
||||
});
|
||||
|
||||
const newSectionIndex = state.sections.findIndex(s => s.name === projectName);
|
||||
if (newSectionIndex !== -1) {
|
||||
|
|
@ -800,10 +802,7 @@ chmod 755 kbtd
|
|||
|
||||
async function createColumn(columnName) {
|
||||
console.log('Creating column:', columnName);
|
||||
const file = await state.fileHandle.getFile();
|
||||
const text = await file.text();
|
||||
const lines = text.split('\n');
|
||||
|
||||
await modifyFileLines(lines => {
|
||||
const section = state.sections[state.currentSectionIndex];
|
||||
|
||||
let insertLine;
|
||||
|
|
@ -834,16 +833,12 @@ chmod 755 kbtd
|
|||
if (insertLine + 1 >= lines.length || lines[insertLine + 1].trim() !== '') {
|
||||
lines.splice(insertLine + 1, 0, '');
|
||||
}
|
||||
|
||||
await writeFile(lines.join('\n'));
|
||||
});
|
||||
}
|
||||
|
||||
async function createCard(columnIndex, cardText) {
|
||||
console.log('Creating card in column', columnIndex, ':', cardText);
|
||||
const file = await state.fileHandle.getFile();
|
||||
const text = await file.text();
|
||||
const lines = text.split('\n');
|
||||
|
||||
await modifyFileLines(lines => {
|
||||
const section = state.sections[state.currentSectionIndex];
|
||||
const column = section.columns[columnIndex];
|
||||
|
||||
|
|
@ -871,8 +866,7 @@ chmod 755 kbtd
|
|||
}
|
||||
|
||||
console.log('Inserted new card at line', insertLine);
|
||||
|
||||
await writeFile(lines.join('\n'));
|
||||
});
|
||||
}
|
||||
|
||||
async function loadFile() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue