Skip to content

Commit

Permalink
feature<applyHeading>:Eliminate one-line limit
Browse files Browse the repository at this point in the history
  • Loading branch information
k4a-l committed Oct 3, 2022
1 parent 36348fd commit f6b26e9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ Download directory(includes `main.js, manifest.json, styles.css`) from the lates

## Features

### Applying Heading [^1]
### Applying Heading

![Applying Heading Demo](https://raw.githubusercontent.com/k4a-dev/obsidian-heading-shifter/main/doc/attachment/applyingHeading.gif)

#### Commands

[^1]: All of Applying Heading commands work when only one line is selected.

| Command | Description | Hotkey |
| ----------------- | ----------------------------------- | ------ |
| Apply Heading 0 | Change Current line to no heading. | - |
Expand Down
15 changes: 7 additions & 8 deletions src/features/applyHeading/operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, Editor } from "obsidian";
import { HeadingShifterSettings } from "settings";
import { composeLineChanges } from "utils/editorChange";
import { createRange } from "utils/range";
import { applyHeading } from "./module";

/** Return obsidian command object : apply heading
Expand All @@ -14,17 +15,15 @@ export const createApplyHeadingCommand = (
): Command => {
const createEditorCallback = (heading: number) => {
return (editor: Editor) => {
// Do not process if multiple lines are selected
if (editor.getCursor("from").line! != editor.getCursor("to").line) {
return;
}
const lines = createRange(
editor.getCursor("from").line,
editor.getCursor("to").line - editor.getCursor("from").line + 1
);

// Dispatch Transaction
editor.transaction({
changes: composeLineChanges(
editor,
[editor.getCursor().line],
(chunk: string) => applyHeading(chunk, heading)
changes: composeLineChanges(editor, lines, (chunk: string) =>
applyHeading(chunk, heading)
),
});
};
Expand Down
3 changes: 3 additions & 0 deletions src/utils/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export const setMax = (prev: undefined | number, cur: number): number => {
}
return prev;
};

export const createRange = (start: number, num: number) =>
Array.from(Array(num), (v, k) => k + start);
8 changes: 8 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
getHeadingLines,
} from "utils/markdown";

import { createRange } from "utils/range";

describe("checkHeading", () => {
test("match", () => {
expect(checkHeading("# content")).toBe(1);
Expand Down Expand Up @@ -151,3 +153,9 @@ f`;
]);
});
});

describe("range", () => {
test("createRange", () => {
expect(createRange(0, 3)).toStrictEqual([0, 1, 2]);
});
});

0 comments on commit f6b26e9

Please sign in to comment.