Skip to content

Commit

Permalink
URL parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Jul 29, 2023
1 parent c770eaf commit 738100a
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 89 deletions.
8 changes: 4 additions & 4 deletions src/Components/ToolsPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ export class ToolsPanel {
//------------
// Render weblinks in page
//------------
/*this.buttons.push(
this.buttons.push(
new ToggleButton(
this.plugin,
()=>this.plugin.settings.showURLs,
(val:boolean)=>this.plugin.settings.showURLs = val,
()=>this.plugin.settings.showURLNodes,
(val:boolean)=>this.plugin.settings.showURLNodes = val,
buttonsWrapperDiv,
{
display: "🌐",
Expand All @@ -247,7 +247,7 @@ export class ToolsPanel {
},
false
)
)*/
)

//------------
//Display siblings
Expand Down
23 changes: 15 additions & 8 deletions src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class Scene {
return;
}

const isFile = !(page.isFolder || page.isTag || page.isVirtual);
const isFile = !(page.isFolder || page.isTag || page.isVirtual || page.isURL);

if(isFile && !page.file) {
this.blockUpdateTimer = false;
Expand All @@ -168,18 +168,20 @@ export class Scene {
keepOnTop(this.plugin.EA);

const centralPage = this.getCentralPage();
const isSameFileAsCurrent = centralPage && isFile && centralPage.file === page.file
const isSameFileAsCurrent = centralPage &&
((isFile && centralPage.file === page.file) ||
(page.isURL && centralPage.isURL && centralPage.url === page.url))

// if the file hasn't changed don't update the graph
if(isSameFileAsCurrent && page.file.stat.mtime === centralPage.mtime) {
if(isSameFileAsCurrent && (page.isURL || (page.file.stat.mtime === centralPage.mtime))) {
this.blockUpdateTimer = false;
return; //don't reload the file if it has not changed
}

if(isFile && shouldOpenFile && !settings.embedCentralNode) {
const centralLeaf = this.getCentralLeaf();
//@ts-ignore
if(!centralLeaf || !app.workspace.getLeafById(centralLeaf.id)) {
if(!centralLeaf || !this.app.workspace.getLeafById(centralLeaf.id)) {
this.centralLeaf = this.ea.openFileInNewOrAdjacentLeaf(page.file);
} else {
centralLeaf.openFile(page.file, {active: false});
Expand All @@ -194,6 +196,11 @@ export class Scene {
this.toolsPanel.rerender();
}

if(page.isURL && !settings.showURLNodes) {
settings.showURLNodes = true;
this.toolsPanel.rerender();
}

if(page.isTag && !settings.showTagNodes) {
settings.showTagNodes = true;
this.toolsPanel.rerender();
Expand Down Expand Up @@ -333,7 +340,7 @@ export class Scene {
}
const frame3 = async () => {
if(this.plugin.settings.allowAutozoom) {
api.zoomToFit(null, 5, 0.15);
setTimeout(()=>api.zoomToFit(null, this.plugin.settings.maxZoom, 0.15),100);
}
ea.targetView.linksAlwaysOpenInANewPane = true;
await this.addEventHandler();
Expand Down Expand Up @@ -394,7 +401,7 @@ export class Scene {
const ea = this.ea;
retainCentralNode =
retainCentralNode && Boolean(this.rootNode) &&
settings.embedCentralNode && isEmbedFileType(centralPage.file,ea);
settings.embedCentralNode && ((centralPage.file && isEmbedFileType(centralPage.file,ea)) || centralPage.isURL);

this.zoomToFitOnNextBrainLeafActivate = !ea.targetView.containerEl.isShown();

Expand Down Expand Up @@ -709,7 +716,7 @@ export class Scene {

excalidrawAPI.updateScene({appState: {viewBackgroundColor: settings.backgroundColor}});
if(settings.allowAutozoom) {
setTimeout(()=>excalidrawAPI.zoomToFit(ea.getViewElements(),5,0.15));
setTimeout(()=>excalidrawAPI.zoomToFit(ea.getViewElements(),settings.maxZoom,0.15),100);
}

this.toolsPanel.rerender();
Expand Down Expand Up @@ -777,7 +784,7 @@ export class Scene {
if(this.zoomToFitOnNextBrainLeafActivate) {
this.zoomToFitOnNextBrainLeafActivate = false;
if(settings.allowAutozoom) {
this.ea.getExcalidrawAPI().zoomToFit(null, 5, 0.15);
this.ea.getExcalidrawAPI().zoomToFit(null, settings.maxZoom, 0.15);
}
}
this.blockUpdateTimer = false;
Expand Down
42 changes: 30 additions & 12 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface ExcaliBrainSettings {
excludeFilepaths: string[];
showInferredNodes: boolean;
showAttachments: boolean;
showURLs: boolean;
showURLNodes: boolean;
showVirtualNodes: boolean;
showFolderNodes: boolean;
showTagNodes: boolean;
Expand All @@ -45,6 +45,7 @@ export interface ExcaliBrainSettings {
baseNodeStyle: NodeStyle;
centralNodeStyle: NodeStyle;
inferredNodeStyle: NodeStyle;
urlNodeStyle: NodeStyle;
virtualNodeStyle: NodeStyle;
siblingNodeStyle: NodeStyle;
attachmentNodeStyle: NodeStyle;
Expand All @@ -69,6 +70,7 @@ export interface ExcaliBrainSettings {
ontologySuggesterMidSentenceTrigger: string;
boldFields: boolean;
allowAutozoom: boolean;
maxZoom: number;
allowAutofocuOnSearch: boolean;
defaultAlwaysOnTop: boolean;
embedCentralNode: boolean;
Expand All @@ -89,7 +91,7 @@ export const DEFAULT_SETTINGS: ExcaliBrainSettings = {
excludeFilepaths: [],
showInferredNodes: true,
showAttachments: true,
showURLs: true,
showURLNodes: true,
showVirtualNodes: true,
showFolderNodes: false,
showTagNodes: false,
Expand All @@ -108,6 +110,9 @@ export const DEFAULT_SETTINGS: ExcaliBrainSettings = {
backgroundColor: "#000005b3",
textColor: "#95c7f3ff",
},
urlNodeStyle: {
prefix: "🌐 "
},
virtualNodeStyle: {
backgroundColor: "#ff000066",
fillStyle: "hachure",
Expand Down Expand Up @@ -156,6 +161,7 @@ export const DEFAULT_SETTINGS: ExcaliBrainSettings = {
ontologySuggesterMidSentenceTrigger: "(",
boldFields: false,
allowAutozoom: true,
maxZoom: 1,
allowAutofocuOnSearch: true,
defaultAlwaysOnTop: false,
embedCentralNode: false,
Expand Down Expand Up @@ -1833,16 +1839,28 @@ export class ExcaliBrainSettingTab extends PluginSettingTab {
this.dirty = true;
}))

new Setting(containerEl)
.setName(t("ALLOW_AUTOFOCUS_ON_SEARCH_NAME"))
.setDesc(fragWithHTML(t("ALLOW_AUTOFOCUS_ON_SEARCH_DESC")))
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.allowAutofocuOnSearch)
.onChange(value => {
this.plugin.settings.allowAutofocuOnSearch = value;
this.dirty = true;
}))
this.numberslider(
containerEl,
t("MAX_AUTOZOOM_NAME"),
t("MAX_AUTOZOOM_DESC"),
{min:10,max:1000, step:10},
()=>this.plugin.settings.maxZoom*100,
(val)=>this.plugin.settings.maxZoom = val/100,
()=>{},
false,
100
)

new Setting(containerEl)
.setName(t("ALLOW_AUTOFOCUS_ON_SEARCH_NAME"))
.setDesc(fragWithHTML(t("ALLOW_AUTOFOCUS_ON_SEARCH_DESC")))
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.allowAutofocuOnSearch)
.onChange(value => {
this.plugin.settings.allowAutofocuOnSearch = value;
this.dirty = true;
}))

new Setting(containerEl)
.setName(t("ALWAYS_ON_TOP_NAME"))
Expand Down
57 changes: 45 additions & 12 deletions src/excalibrain-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { WarningPrompt } from './utils/Prompts';
import { FieldSuggester } from './Suggesters/OntologySuggester';
import { Literal } from 'obsidian-dataview/lib/data-model/value';
import { isEmbedFileType } from './utils/fileUtils';
import { URLParser } from './graph/URLParser';

declare module "obsidian" {
interface App {
Expand Down Expand Up @@ -56,6 +57,7 @@ export default class ExcaliBrain extends Plugin {
private focusSearchAfterInitiation:boolean = false;
public customNodeLabel: (dvPage: Literal, defaultName:string) => string
public navigationHistory: string[] = [];
public urlParser: URLParser;

constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
Expand All @@ -74,11 +76,13 @@ export default class ExcaliBrain extends Plugin {
this.navigationHistory = this.settings.navigationHistory;
this.addSettingTab(new ExcaliBrainSettingTab(this.app, this));
this.registerEditorSuggest(new FieldSuggester(this));
this.urlParser = new URLParser(this);
this.app.workspace.onLayoutReady(()=>{
this.urlParser.init();
this.DVAPI = getAPI();
if(!this.DVAPI) {
(new WarningPrompt(
app,
this.app,
"⚠ ExcaliBrain Disabled: DataView Plugin not found",
t("DATAVIEW_NOT_FOUND"))
).show(async (result: boolean) => {
Expand All @@ -90,7 +94,7 @@ export default class ExcaliBrain extends Plugin {
}
if(!this.DVAPI.version.compare('>=', '0.5.31')) {
(new WarningPrompt(
app,
this.app,
"⚠ ExcaliBrain Disabled: Dataview upgrade requried",
t("DATAVIEW_UPGRADE"))
).show(async (result: boolean) => {
Expand Down Expand Up @@ -153,6 +157,19 @@ export default class ExcaliBrain extends Plugin {
await sleep(100);
}

counter = 0;
while(!this.urlParser.initalized) {
if(counter++ % 100 === 10) {
new Notice("ExcaliBrain is waiting for URLParser to finish indexing",1000);
}
await sleep(100);
}

//Add all host urls
this.urlParser.hosts.forEach((url)=>{
this.pages.add(url, new Page(this.pages, url, null, this, false, false, url, url));
});

//Add all folders and files
const addFolderChildren = (parentFolder: TFolder, parent: Page) => {
const children = parentFolder.children;
Expand Down Expand Up @@ -208,13 +225,17 @@ export default class ExcaliBrain extends Plugin {
//Add all links as inferred children to pages on which they were found
this.pages.addResolvedLinks();

//Add all urls as inferred children to pages on which they were found
//and inferred children of their origins
this.pages.addPageURLs();

const self = this;
setTimeout(async()=>{
//@ts-ignore
const bookmarksPlugin = app.internalPlugins.getPluginById("bookmarks");
const bookmarksPlugin = this.app.internalPlugins.getPluginById("bookmarks");
if(!bookmarksPlugin) { //code to be removed when bookmarks plugin is released, only leave return
//@ts-ignore
const starredPlugin = app.internalPlugins.getPluginById("starred");
const starredPlugin = this.app.internalPlugins.getPluginById("starred");
if(!starredPlugin) {
return;
}
Expand Down Expand Up @@ -572,7 +593,7 @@ export default class ExcaliBrain extends Plugin {
}

this.EA.onLinkClickHook = (element,linkText,event) => {
const path = linkText.match(/\[\[([^\]]*)/)?.[1];
const path = linkText.match(/\[\[([^\]]*)/)?.[1] ?? linkText.match(/(http.*)/)?.[1];
if(!path) return true;
const page = this.pages.get(path);
const ea = this.EA;
Expand Down Expand Up @@ -605,15 +626,20 @@ export default class ExcaliBrain extends Plugin {
return false;
}

//if centralPage is in embeddedFrame, simply render the scene
//if centralPage is in embeddedFrame
if(this.settings.embedCentralNode) {
//the user clicked the link handle in the top left, then open the file in a leaf
if(this.scene.centralPagePath === page.path) {
if(this.scene.isCentralLeafStillThere()) {
this.scene.centralLeaf.openFile(page.file,{active:true});
return false;
if(page.isURL) {
return true; //let Excalidraw open the webpage
} else {
if(this.scene.isCentralLeafStillThere()) {
this.scene.centralLeaf.openFile(page.file,{active:true});
return false;
}
ea.targetView.linksAlwaysOpenInANewPane = false;
setTimeout(()=>ea.targetView.linksAlwaysOpenInANewPane = true,300);
}
ea.targetView.linksAlwaysOpenInANewPane = false;
setTimeout(()=>ea.targetView.linksAlwaysOpenInANewPane = true,300);
return true;
}
this.scene.renderGraphForPath(path);
Expand All @@ -622,7 +648,7 @@ export default class ExcaliBrain extends Plugin {

const centralLeaf = this.scene.getCentralLeaf();
//handle click on link to existing file
if(!page.isFolder && !page.isTag) {
if(!page.isFolder && !page.isTag && !page.isURL) {
//if the leaf attached to ExcaliBrain already has the new file open, render the associated graph
if((centralLeaf?.view as TextFileView)?.file?.path === path) {
this.scene.renderGraphForPath(path);
Expand Down Expand Up @@ -831,6 +857,13 @@ export default class ExcaliBrain extends Plugin {
display: t("NODESTYLE_INFERRED"),
getInheritedStyle: ()=> this.settings.baseNodeStyle
};
this.nodeStyles["url"] = {
style: this. settings.urlNodeStyle,
allowOverride: true,
userStyle: false,
display: t("NODESTYLE_URL"),
getInheritedStyle: ()=> this.settings.baseNodeStyle
},
this.nodeStyles["virtual"] = {
style: this.settings.virtualNodeStyle,
allowOverride: true,
Expand Down
Loading

0 comments on commit 738100a

Please sign in to comment.