generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Insert heading at current, higher and deeper level (#10)
* Add v0 implementation of 'insert heading' feature * Fix some little index bugs * Fix conditional line assignment * Add additional test * doc: add document of insert headings Co-authored-by: kasahala <[email protected]>
- Loading branch information
Showing
6 changed files
with
229 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createInsertHeadingAtCurrentLevelCommand, createInsertHeadingAtDeeperLevelCommand, createInsertHeadingAtHigherLevelCommand } from "./operation"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { applyHeading } from "features/applyHeading"; | ||
import { Command, Editor, Notice } from "obsidian"; | ||
import { composeLineChanges } from "utils/editorChange"; | ||
import { checkHeading, getPreviousHeading } from "utils/markdown"; | ||
|
||
export const createInsertHeadingAtCurrentLevelCommand = (): Command => { | ||
const createEditorCallback = () => { | ||
return (editor: Editor) => { | ||
|
||
const cursorLine = editor.getCursor("from").line | ||
const lastHeadingLine = getPreviousHeading(editor, cursorLine) | ||
|
||
// current heading level == most recently added heading | ||
// 0 if no heading exists yet | ||
const headingLevel = lastHeadingLine != undefined ? checkHeading(editor.getLine(lastHeadingLine)) : 0 | ||
|
||
editor.transaction({ | ||
changes: composeLineChanges(editor, [cursorLine], (chunk: string) => | ||
applyHeading(chunk, headingLevel) | ||
), | ||
}); | ||
|
||
editor.setCursor(editor.getCursor().line) | ||
return | ||
}; | ||
}; | ||
|
||
// return command object | ||
return { | ||
id: `insert-heading-current`, | ||
name: `Insert Heading at current level`, | ||
icon: `headingShifter_heading`, | ||
editorCallback: createEditorCallback(), | ||
}; | ||
}; | ||
|
||
|
||
export const createInsertHeadingAtDeeperLevelCommand = (): Command => { | ||
const createEditorCallback = () => { | ||
return (editor: Editor) => { | ||
|
||
const cursorLine = editor.getCursor("from").line | ||
const lastHeadingLine = getPreviousHeading(editor, cursorLine) | ||
|
||
// current heading level == most recently added heading | ||
// 0 if no heading exists yet | ||
const headingLevel = lastHeadingLine ? checkHeading(editor.getLine(lastHeadingLine)) : 0 | ||
|
||
if (headingLevel+1 > 6) { | ||
new Notice("Cannot Increase (contains more than Heading 6)"); | ||
return true; | ||
} | ||
|
||
editor.transaction({ | ||
changes: composeLineChanges(editor, [cursorLine], (chunk: string) => | ||
applyHeading(chunk, headingLevel + 1) | ||
), | ||
}); | ||
|
||
editor.setCursor(editor.getCursor().line) | ||
return | ||
}; | ||
}; | ||
|
||
// return command object | ||
return { | ||
id: `insert-heading-deeper`, | ||
name: `Insert Heading at one level deeper`, | ||
icon: `headingShifter_heading`, | ||
editorCallback: createEditorCallback(), | ||
}; | ||
}; | ||
|
||
export const createInsertHeadingAtHigherLevelCommand = (): Command => { | ||
const createEditorCallback = () => { | ||
return (editor: Editor) => { | ||
|
||
const cursorLine = editor.getCursor("from").line | ||
const lastHeadingLine = getPreviousHeading(editor, cursorLine) | ||
|
||
// current heading level == most recently added heading | ||
// 0 if no heading exists yet | ||
const headingLevel = lastHeadingLine ? checkHeading(editor.getLine(lastHeadingLine)) : 0 | ||
|
||
editor.transaction({ | ||
changes: composeLineChanges(editor, [cursorLine], (chunk: string) => | ||
applyHeading(chunk, headingLevel - 1) | ||
), | ||
}); | ||
|
||
editor.setCursor(editor.getCursor().line) | ||
return | ||
}; | ||
}; | ||
|
||
// return command object | ||
return { | ||
id: `insert-heading-higher`, | ||
name: `Insert Heading at one level higher`, | ||
icon: `headingShifter_heading`, | ||
editorCallback: createEditorCallback(), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters