Skip to content

Commit

Permalink
feat: lightspeed
Browse files Browse the repository at this point in the history
Also fixed a bug concerning source and legacy editor mode names (Obsidian v0.15.0).

Original code from @mrjackphil and @kohane27, cf.
mrjackphil#35
  • Loading branch information
kometenstaub committed Jun 15, 2022
1 parent a19f27f commit 128ef7a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
80 changes: 65 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {App, Plugin, PluginSettingTab, Setting, View} from 'obsidian';
import {App, MarkdownView, Plugin, PluginSettingTab, Setting, View} from 'obsidian';
import {Editor} from 'codemirror';
import {EditorSelection} from "@codemirror/state";
import {EditorView, ViewPlugin} from "@codemirror/view";
Expand All @@ -12,9 +12,9 @@ import LegacySourceLinkProcessor from "./processors/LegacySourceLinkProcessor";
import PreviewLinkProcessor from "./processors/PreviewLinkProcessor";

enum VIEW_MODE {
LEGACY,
SOURCE,
PREVIEW,
CM6
LEGACY
}

export default class JumpToLink extends Plugin {
Expand Down Expand Up @@ -49,13 +49,20 @@ export default class JumpToLink extends Plugin {
callback: this.action.bind(this, 'regexp'),
hotkeys: [{modifiers: ["Ctrl"], key: ";"}],
});

this.addCommand({
id: "activate-lightspeed-jump",
name: "Lightspeed Jump",
callback: this.action.bind(this, 'lightspeed'),
hotkeys: [],
});
}

onunload() {
console.log('unloading jump to links plugin');
}

action(type: 'link' | 'regexp') {
action(type: 'link' | 'regexp' | 'lightspeed') {
if (this.isLinkHintActive) {
return;
}
Expand All @@ -67,6 +74,9 @@ export default class JumpToLink extends Plugin {
case "regexp":
this.handleJumpToRegex();
return
case "lightspeed":
this.handleLightspeedJump();
return
}
}

Expand All @@ -76,19 +86,18 @@ export default class JumpToLink extends Plugin {

if (currentView.getState().mode === 'preview') {
return VIEW_MODE.PREVIEW;
} else if (!isLegacy) {
return VIEW_MODE.CM6;
} else if (currentView.getState().mode === 'source') {
} else if (isLegacy) {
return VIEW_MODE.LEGACY;
} else if (currentView.getState().mode === 'source') {
return VIEW_MODE.SOURCE;
}

return VIEW_MODE.LEGACY;
}

handleJumpToLink = () => {
const {settings: {letters}, app} = this

const currentView = app.workspace.activeLeaf.view;
const currentView = app.workspace.getLeaf(false).view;
const mode = this.getMode(currentView);

switch (mode) {
Expand All @@ -102,7 +111,7 @@ export default class JumpToLink extends Plugin {
const previewLinkHints = new PreviewLinkProcessor(previewViewEl, letters).init();
this.handleActions(previewLinkHints);
break;
case VIEW_MODE.CM6:
case VIEW_MODE.SOURCE:
const cm6Editor: EditorView = (<{ editor?: { cm: EditorView } }>currentView).editor.cm;
const livePreviewLinks = new CM6LinkProcessor(cm6Editor, letters).init();
this.markPlugin.setLinks(livePreviewLinks);
Expand All @@ -112,15 +121,16 @@ export default class JumpToLink extends Plugin {
}
}

handleJumpToRegex = () => {
handleJumpToRegex = (stringToSearch?: string) => {
const {app, settings: {letters, jumpToAnywhereRegex}} = this
const currentView = app.workspace.activeLeaf.view;
const currentView = app.workspace.getLeaf(false).view
const mode = this.getMode(currentView);
const whatToLookAt = stringToSearch || jumpToAnywhereRegex;

switch (mode) {
case VIEW_MODE.CM6:
case VIEW_MODE.SOURCE:
const cm6Editor: EditorView = (<{ editor?: { cm: EditorView } }>currentView).editor.cm;
const livePreviewLinks = new CM6RegexProcessor(cm6Editor, letters, jumpToAnywhereRegex).init();
const livePreviewLinks = new CM6RegexProcessor(cm6Editor, letters, whatToLookAt).init();
this.markPlugin.setLinks(livePreviewLinks);
this.app.workspace.updateOptions();
this.handleActions(livePreviewLinks, cm6Editor);
Expand All @@ -129,14 +139,54 @@ export default class JumpToLink extends Plugin {
break;
case VIEW_MODE.LEGACY:
const cmEditor: Editor = (currentView as any).sourceMode.cmEditor;
const links = new LegacyRegexpProcessor(cmEditor, jumpToAnywhereRegex, letters).init();
const links = new LegacyRegexpProcessor(cmEditor, whatToLookAt, letters).init();
this.handleActions(links, cmEditor);
break;
default:
break;
}

}
// adapted from: https:/mrjackphil/obsidian-jump-to-link/issues/35#issuecomment-1085905668
handleLightspeedJump() {
// get all text color
const { contentEl } = app.workspace.getActiveViewOfType(MarkdownView)
if (!contentEl) {return}
const contentContainerColor = contentEl.getElementsByClassName("cm-contentContainer");
const originalColor = contentContainerColor[0].style.color;
// change all text color to gray
contentContainerColor[0].style.color = 'var(--jump-to-link-lightspeed-color)';

const keyArray = []
const grabKey = (event) => {
event.preventDefault();
// handle Escape to reject the mode
if (event.key === 'Escape') {
document.removeEventListener("keydown", grabKey, { capture: true })
}

// test if keypress is capitalized
if (/^[a-z]$/i.test(event.key)) {
const isCapital = event.shiftKey;
if (isCapital) {
// capture uppercase
keyArray.push((event.key).toUpperCase());
} else {
// capture lowercase
keyArray.push(event.key);
}
}

// stop when length of array is equal to 2
if (keyArray.length === 2) {
this.handleJumpToRegex("\\b" + keyArray.join(""));
// removing eventListener after proceeded
document.removeEventListener("keydown", grabKey, { capture: true })
contentContainerColor[0].style.color = originalColor;
}
}
document.addEventListener('keydown', grabKey, { capture: true });
}

handleActions = (linkHints: LinkHintBase[], cmEditor?: Editor | EditorView): void => {
if (!linkHints.length) {
Expand Down
5 changes: 4 additions & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
color: black;
padding: 1px 3px;
border-radius: 3px;
}
}
.theme-dark, .theme-light {
--jump-to-link-lightspeed-color: var(--text-muted);
}

2 comments on commit 128ef7a

@mrjackphil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks cool. I'm hoping to test it as soon as I can.
If you'll make a PR I would suggest to provide an extended README to reflect the lightspeed functionality.

@kometenstaub
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The latest version supports Obsidian v0.15.0 as well (including jump to link and anywhere) in extra windows and I fixed a bug with the cm6 plugin.

Here is the branch: https:/kometenstaub/obsidian-jump-to-link/tree/lightspeed

Please sign in to comment.