1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/packages/app-cli/app/LinkSelector.ts

142 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-06-18 23:50:19 +02:00
const open = require('open');
interface LinkStoreEntry {
link: string;
noteX: number;
noteY: number;
}
2020-05-23 15:23:55 +02:00
class LinkSelector {
private noteId_: string;
private scrollTop_: number;
private renderedText_: string;
private currentLinkIndex_: number;
private linkStore_: LinkStoreEntry[];
private linkRegex_: RegExp;
public constructor() {
2020-05-24 10:38:14 +02:00
this.noteId_ = null;
this.scrollTop_ = null; // used so 'o' won't open unhighlighted link after scrolling
this.renderedText_ = null;
this.currentLinkIndex_ = null;
this.linkStore_ = null;
this.linkRegex_ = /http:\/\/[0-9.]+:[0-9]+\/[0-9]+/g;
2020-05-23 15:23:55 +02:00
}
public get link(): string | null {
if (this.currentLinkIndex_ === null) return null;
return this.linkStore_[this.currentLinkIndex_].link;
}
public get noteX(): number | null {
if (this.currentLinkIndex_ === null) return null;
return this.linkStore_[this.currentLinkIndex_].noteX;
}
public get noteY(): number | null {
if (this.currentLinkIndex_ === null) return null;
return this.linkStore_[this.currentLinkIndex_].noteY;
}
public findLinks(renderedText: string): LinkStoreEntry[] {
const newLinkStore: LinkStoreEntry[] = [];
const lines: string[] = renderedText.split('\n');
for (let i = 0; i < lines.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-11-05 18:58:23 +02:00
const r = (lines[i] as any).matchAll(this.linkRegex_);
const matches = [...r];
2023-06-30 10:39:21 +02:00
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
matches.forEach((_e, n) => {
newLinkStore.push(
{
link: matches[n][0],
noteX: matches[n].index,
noteY: i,
},
);
});
}
return newLinkStore;
}
public updateText(renderedText: string): void {
this.currentLinkIndex_ = null;
2020-05-23 15:23:55 +02:00
this.renderedText_ = renderedText;
this.linkStore_ = this.findLinks(this.renderedText_);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public updateNote(textWidget: any): void {
this.noteId_ = textWidget.noteId;
this.scrollTop_ = textWidget.scrollTop_;
this.updateText(textWidget.renderedText_);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public scrollWidget(textWidget: any): void {
2020-05-24 10:38:14 +02:00
if (this.currentLinkIndex_ === null) return;
const noteY = this.linkStore_[this.currentLinkIndex_].noteY;
2020-05-24 10:38:14 +02:00
let viewBoxMin = textWidget.scrollTop_ + 1;
let viewBoxMax = viewBoxMin + textWidget.innerHeight - 2;
if (noteY < viewBoxMin) {
for (; noteY < viewBoxMin; textWidget.pageUp()) {
viewBoxMin = textWidget.scrollTop_;
viewBoxMax = viewBoxMin + textWidget.innerHeight;
}
return;
} else if (noteY > viewBoxMax) {
for (; noteY > viewBoxMax; textWidget.pageDown()) {
viewBoxMin = textWidget.scrollTop_;
viewBoxMax = viewBoxMin + textWidget.innerHeight;
}
return;
}
return;
2020-05-23 15:23:55 +02:00
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public changeLink(textWidget: any, offset: number): void | null {
2020-05-23 15:23:55 +02:00
if (textWidget.noteId !== this.noteId_) {
this.updateNote(textWidget);
this.changeLink(textWidget, offset);
2020-05-23 15:23:55 +02:00
return;
}
if (textWidget.renderedText_ !== this.renderedText_) {
this.updateText(textWidget.renderedText_);
this.changeLink(textWidget, offset);
2020-05-23 15:23:55 +02:00
return;
}
2020-05-24 10:38:14 +02:00
if (textWidget.scrollTop_ !== this.scrollTop_) this.scrollTop_ = textWidget.scrollTop_;
2020-05-23 15:23:55 +02:00
if (!this.linkStore_.length) return null;
let offsetMod = (offset + this.currentLinkIndex_) % this.linkStore_.length;
2020-05-23 15:23:55 +02:00
if (this.currentLinkIndex_ === null) {
if (offsetMod < 0) this.currentLinkIndex_ = this.linkStore_.length + offsetMod;
else if (!offsetMod) this.currentLinkIndex_ = 0;
else this.currentLinkIndex_ = offsetMod - 1;
return;
}
2020-05-23 15:23:55 +02:00
2020-05-24 10:38:14 +02:00
if (offsetMod < 0) offsetMod = this.linkStore_.length + offsetMod;
this.currentLinkIndex_ = offsetMod;
return;
2020-05-23 15:23:55 +02:00
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public openLink(textWidget: any): void {
2020-05-23 15:23:55 +02:00
if (textWidget.noteId !== this.noteId_) return;
if (textWidget.renderedText_ !== this.renderedText_) return;
if (textWidget.scrollTop_ !== this.scrollTop_) return;
open(this.linkStore_[this.currentLinkIndex_].link);
2020-05-23 15:23:55 +02:00
}
}
export default LinkSelector;