Skip to content

Commit

Permalink
Fixed: Pressing enter at the start of a line creates an unwanted bull…
Browse files Browse the repository at this point in the history
…et point #156
  • Loading branch information
vslinko committed Jun 5, 2021
1 parent d41221b commit c8bd31d
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 5 deletions.
2 changes: 2 additions & 0 deletions jest/obsidian-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module.exports = class CustomEnvironment extends NodeEnvironment {
this.runCommand("simulateKeydown", data);
this.global.executeCommandById = (data) =>
this.runCommand("executeCommandById", data);
this.global.setSetting = (data) => this.runCommand("setSetting", data);
this.global.resetSettings = () => this.runCommand("resetSettings");
this.global.parseState = (data) => this.runCommand("parseState", data);
this.global.getCurrentState = () => this.runCommand("getCurrentState");
}
Expand Down
2 changes: 2 additions & 0 deletions jest/test-globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ declare function parseState(state: string): Promise<IState>;
declare function parseState(state: string[]): Promise<IState>;
declare function simulateKeydown(keys: string): Promise<void>;
declare function executeCommandById(keys: string): Promise<void>;
declare function setSetting(opts: { k: string; v: any }): Promise<void>;
declare function resetSettings(): Promise<void>;
declare function getCurrentState(): Promise<IState>;
12 changes: 12 additions & 0 deletions specs/ensure-cursor-in-list-content.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
- |one
```

# cursor should not be moved to list content if stickCursor=false

- setting: `stickCursor=false`

```md
|- one
```

```md
|- one
```

# cursor should be moved to list content after arrowup

```md
Expand Down
17 changes: 17 additions & 0 deletions specs/enter.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,20 @@
```
- two
```

# enter should not create new item if cursor is before line start

- setting: `stickCursor=false`

```md
- one
|- two
```

- keydown: `Enter`

```md
- one

|- two
```
27 changes: 27 additions & 0 deletions specs/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ interface IExecuteCommandById {
command: string;
}

interface ISetSetting {
type: "setSetting";
k: string;
v: any;
}

type Action = ISimulateKeydown | IExecuteCommandById;

interface ITestDesc {
title: string;
before: string[];
actions: Action[];
settings: ISetSetting[];
after: string[];
}

Expand All @@ -30,13 +37,18 @@ function makeTestDesc(): ITestDesc {
title: "",
before: [],
actions: [],
settings: [],
after: [],
};
}

function registerTest(desc: ITestDesc) {
test(desc.title, async () => {
// arrange
await resetSettings();
for (const action of desc.settings) {
await setSetting({ k: action.k, v: action.v });
}
await applyState(desc.before);

// act
Expand Down Expand Up @@ -95,6 +107,21 @@ for (const file of files) {
type: "executeCommandById",
command: line.replace(/^- execute: `/, "").slice(0, -1),
});
} else if (
sm === "looking-for-before" &&
/^- setting: `[^`]+`$/.test(line)
) {
const content = line
.replace(/^- setting: `/, "")
.slice(0, -1)
.split("=", 2);
const k = content[0];
const v = JSON.parse(content[1]);
desc.settings.push({
type: "setSetting",
k,
v,
});
} else if (sm === "looking-for-actions" && line.startsWith("```")) {
sm = "inside-after";
} else if (sm === "inside-after" && line.startsWith("```")) {
Expand Down
2 changes: 1 addition & 1 deletion src/ObsidianOutlinerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ShiftEnterShouldCreateNoteFeature } from "./features/ShiftEnterShouldCr

export default class ObsidianOutlinerPlugin extends Plugin {
private features: IFeature[];
private settingsService: SettingsService;
protected settingsService: SettingsService;
private loggerService: LoggerService;
private obsidianService: ObsidianService;
private listsService: ListsService;
Expand Down
25 changes: 24 additions & 1 deletion src/ObsidianOutlinerPluginWithTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MarkdownView } from "obsidian";
import ObsidianOutlinerPlugin from "./ObsidianOutlinerPlugin";
import { ObsidianOutlinerPluginSettings } from "./services/SettingsService";

const keysMap: { [key: string]: number } = {
Backspace: 8,
Expand All @@ -23,6 +24,22 @@ export default class ObsidianOutlinerPluginWithTests extends ObsidianOutlinerPlu
(this.app as any).commands.executeCommandById(id);
}

async setSetting<T extends keyof ObsidianOutlinerPluginSettings>({
k,
v,
}: {
k: T;
v: ObsidianOutlinerPluginSettings[T];
}) {
this.settingsService[k] = v;
await this.settingsService.save();
}

async resetSettings() {
this.settingsService.reset();
await this.settingsService.save();
}

simulateKeydown(keys: string) {
const e = {
type: "keydown",
Expand Down Expand Up @@ -119,7 +136,7 @@ export default class ObsidianOutlinerPluginWithTests extends ObsidianOutlinerPlu

const ws = new WebSocket("ws://127.0.0.1:8080/");

ws.addEventListener("message", (event) => {
ws.addEventListener("message", async (event) => {
const { id, type, data } = JSON.parse(event.data);

let result;
Expand All @@ -136,6 +153,12 @@ export default class ObsidianOutlinerPluginWithTests extends ObsidianOutlinerPlu
case "executeCommandById":
this.executeCommandById(data);
break;
case "resetSettings":
await this.resetSettings();
break;
case "setSetting":
await this.setSetting(data);
break;
case "parseState":
result = this.parseState(data);
break;
Expand Down
5 changes: 5 additions & 0 deletions src/operations/CreateNewItemOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export class CreateNewItemOperation implements IOperation {
}

const cursor = root.getCursor();
const lineUnderCursor = lines.find((l) => l.from.line === cursor.line);

if (cursor.ch < lineUnderCursor.from.ch) {
return;
}

const { oldLines, newLines } = lines.reduce(
(acc, line) => {
Expand Down
13 changes: 10 additions & 3 deletions src/operations/CreateNoteLineOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ export class CreateNoteLineOperation implements IOperation {
return;
}

this.stopPropagation = true;
this.updated = true;

const cursor = root.getCursor();
const list = root.getListUnderCursor();
const lineUnderCursor = list
.getLinesInfo()
.find((l) => l.from.line === cursor.line);

if (cursor.ch < lineUnderCursor.from.ch) {
return;
}

this.stopPropagation = true;
this.updated = true;

if (!list.getNotesIndent()) {
const indent = list.isEmpty()
Expand Down
6 changes: 6 additions & 0 deletions src/services/SettingsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export class SettingsService implements ObsidianOutlinerPluginSettings {
}
}

reset() {
for (const [k, v] of Object.entries(DEFAULT_SETTINGS)) {
this.set(k, v);
}
}

async load() {
this.values = Object.assign(
{},
Expand Down

0 comments on commit c8bd31d

Please sign in to comment.