Skip to content

Commit 0366767

Browse files
committed
Several improvements
1 parent 3822728 commit 0366767

File tree

4 files changed

+72
-32
lines changed

4 files changed

+72
-32
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ If you want to forget the text direction of a file and go back to using the defa
3838
- This plugin only treats the Markdown editor, preview and export. There are some areas of the app, like the Outline view, that are not covered for now.
3939
- Auto-pair brackets is disabled in RTL ([CodeMirror bug with a pending fix](https://github.com/esm7/obsidian-rtl/issues/7)).
4040
- When an RTL line ends with an LTR word, the End key misbehaves. [CodeMirror bug](https://github.com/codemirror/CodeMirror/issues/6531).
41+
- There are various glitches in handling some keyboard shortcuts in RTL, e.g. Ctrl+Home/Ctrl+End are not properly handled yet, Ctrl+Left lands on the wrong character and some others. These are very hard to overcome due to the patchy RTL support of the current CodeMirror versions, and occasionally I'm able to improve things a bit.
4142

4243
## Changelog
4344

45+
### 0.0.7
46+
47+
- Improved the handling of the Home & End keys (without Shift at this point) in RTL. It's still not perfect (CodeMirror is not good at this) but at least the basic functionality works.
48+
- Some adaptations to the newest Obsidian API.
49+
- Setting the direction of the note title (https://github.com/esm7/obsidian-rtl/issues/15) is now configurable.
50+
4451
### 0.0.6
4552

4653
- Lists are finally rendered properly in RTL, both in Markdown and in Preview!

main.ts

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { App, Modal, MarkdownView, Plugin, PluginSettingTab, TFile, TAbstractFile, Setting } from 'obsidian';
1+
import { App, Editor, MarkdownView, Plugin, PluginSettingTab, TFile, TAbstractFile, Setting } from 'obsidian';
2+
import * as codemirror from 'codemirror';
23

34
class Settings {
45
public fileDirections: { [path: string]: string } = {};
56
public defaultDirection: string = 'ltr';
67
public rememberPerFile: boolean = true;
8+
public setNoteTitleDirection: boolean = true;
79

810
toJson() {
911
return JSON.stringify(this);
@@ -14,6 +16,7 @@ class Settings {
1416
this.fileDirections = obj['fileDirections'];
1517
this.defaultDirection = obj['defaultDirection'];
1618
this.rememberPerFile = obj['rememberPerFile'];
19+
this.setNoteTitleDirection = obj['setNoteTitleDirection'];
1720
}
1821
}
1922

@@ -61,22 +64,34 @@ export default class RtlPlugin extends Plugin {
6164
}
6265
}));
6366

64-
this.registerDomEvent(document, 'keydown', (ev: KeyboardEvent) => {
65-
// Patch Home/End issue on RTL: https://github.com/esm7/obsidian-rtl/issues/6
66-
if (ev.key == 'End' || ev.key == 'Home') {
67-
let cmEditor = this.getEditor();
68-
if (cmEditor.getOption("direction") == 'rtl') {
69-
// In theory we can execute the following regardless of the editor direction, it should always work,
70-
// but it's redundant and the principle in this plugin is to not interfere with Obsidian when the
71-
// direction is LTR
72-
if (ev.key == 'End') {
73-
cmEditor.execCommand('goLineEnd');
74-
} else if (ev.key == 'Home') {
75-
cmEditor.execCommand('goLineStartSmart');
67+
this.registerCodeMirror((cm: CodeMirror.Editor) => {
68+
let cmEditor = cm;
69+
cmEditor.setOption('extraKeys', {
70+
'End': (cm) => {
71+
if (cm.getOption('direction') == 'rtl') {
72+
let editor = this.getObsidianEditor();
73+
let pos = editor.getCursor();
74+
pos.ch = 1000;
75+
editor.setCursor(pos);
76+
editor.refresh();
7677
}
78+
else
79+
cm.execCommand('goLineEnd');
80+
},
81+
'Home': (cm) => {
82+
if (cm.getOption('direction') == 'rtl') {
83+
let editor = this.getObsidianEditor();
84+
let pos = editor.getCursor();
85+
pos.ch = 0;
86+
editor.setCursor(pos);
87+
editor.refresh();
88+
}
89+
else
90+
cm.execCommand('goLineStartSmart');
7791
}
78-
}
92+
});
7993
});
94+
8095
}
8196

8297
onunload() {
@@ -107,24 +122,28 @@ export default class RtlPlugin extends Plugin {
107122
catch(error => { console.log("RTL settings file not found"); });
108123
}
109124

110-
getEditor() {
111-
var view = this.app.workspace.activeLeaf.view;
112-
if (view.getViewType() == 'markdown') {
113-
var markdownView = view as MarkdownView;
114-
var cmEditor = markdownView.sourceMode.cmEditor;
115-
return cmEditor;
116-
}
125+
getObsidianEditor(): Editor {
126+
let view = this.app.workspace.getActiveViewOfType(MarkdownView);
127+
if (view)
128+
return view.editor;
129+
return null;
130+
}
131+
132+
getCmEditor(): codemirror.Editor {
133+
let view = this.app.workspace.getActiveViewOfType(MarkdownView);
134+
if (view)
135+
return view.sourceMode?.cmEditor;
117136
return null;
118137
}
119138

120139
setDocumentDirection(newDirection: string) {
121-
var cmEditor = this.getEditor();
140+
var cmEditor = this.getCmEditor();
122141
if (cmEditor && cmEditor.getOption("direction") != newDirection) {
123142
this.patchAutoCloseBrackets(cmEditor, newDirection);
124-
cmEditor.setOption("direction", newDirection);
143+
cmEditor.setOption("direction", newDirection as any);
125144
cmEditor.setOption("rtlMoveVisually", true);
126145
}
127-
var view = this.app.workspace.activeLeaf.view;
146+
let view = this.app.workspace.getActiveViewOfType(MarkdownView);
128147
if (view && view.previewMode && view.previewMode.containerEl)
129148
view.previewMode.containerEl.dir = newDirection;
130149

@@ -133,11 +152,13 @@ export default class RtlPlugin extends Plugin {
133152
document.head.appendChild(listStyle);
134153
listStyle.sheet.insertRule(".CodeMirror-rtl pre { text-indent: 0px !important; }");
135154

136-
var leafContainer = (this.app.workspace.activeLeaf as any).containerEl as Document;
137-
let header = leafContainer.getElementsByClassName('view-header-title-container');
138-
// let headerStyle = document.createElement('style');
139-
// header[0].appendChild(headerStyle);
140-
(header[0] as any).style.direction = newDirection;
155+
if (this.settings.setNoteTitleDirection) {
156+
var leafContainer = (this.app.workspace.activeLeaf as any).containerEl as Document;
157+
let header = leafContainer.getElementsByClassName('view-header-title-container');
158+
// let headerStyle = document.createElement('style');
159+
// header[0].appendChild(headerStyle);
160+
(header[0] as any).style.direction = newDirection;
161+
}
141162

142163
this.setExportDirection(newDirection);
143164
}
@@ -164,7 +185,7 @@ export default class RtlPlugin extends Plugin {
164185
}
165186

166187
toggleDocumentDirection() {
167-
var cmEditor = this.getEditor();
188+
var cmEditor = this.getCmEditor();
168189
if (cmEditor) {
169190
var newDirection = cmEditor.getOption("direction") == "ltr" ? "rtl" : "ltr"
170191
this.setDocumentDirection(newDirection);
@@ -214,5 +235,16 @@ class RtlSettingsTab extends PluginSettingTab {
214235
this.plugin.saveSettings();
215236
this.plugin.adjustDirectionToCurrentFile();
216237
}));
238+
239+
new Setting(containerEl)
240+
.setName('Set note title direction')
241+
.setDesc('In RTL notes, also set the direction of the note title.')
242+
.addToggle(toggle => toggle.setValue(this.settings.setNoteTitleDirection)
243+
.onChange((value) => {
244+
this.settings.setNoteTitleDirection = value;
245+
this.plugin.saveSettings();
246+
this.plugin.adjustDirectionToCurrentFile();
247+
}));
248+
217249
}
218250
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-rtl",
33
"name": "RTL Support",
4-
"version": "0.0.6",
4+
"version": "0.0.7",
55
"description": "Right to Left (RTL) text direction support for languages like Arabic, Hebrew and Farsi.",
66
"author": "esm",
77
"authorUrl": "",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-rtl",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "RTL (Right to Left) language support for Obsidian.",
55
"main": "main.js",
66
"scripts": {
@@ -15,6 +15,7 @@
1515
"@rollup/plugin-node-resolve": "^9.0.0",
1616
"@rollup/plugin-typescript": "^6.1.0",
1717
"@types/node": "^14.14.2",
18+
"codemirror": "^5.62.0",
1819
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
1920
"rollup": "^2.33.0",
2021
"tslib": "^2.0.3",

0 commit comments

Comments
 (0)