Skip to content

Commit

Permalink
Merge pull request #43 from mrparalon/refactor-filtering
Browse files Browse the repository at this point in the history
Change filtering to hidden nodes instead of drop
  • Loading branch information
trashhalo authored May 14, 2023
2 parents 868c0e7 + 1f46f29 commit a13cc86
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
46 changes: 21 additions & 25 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
import Edge from "./Edge.svelte";
import Settings from "./settings/Settings.svelte";
import Sigma from "./Sigma.svelte";
import { uiVisible, store, graph, settings, Mode, NodeFilter } from "./stores";
import {
uiVisible,
store,
graph,
settings,
Mode,
NodeFilter,
} from "./stores";
onMount(async () => {
// @ts-ignore
Expand All @@ -18,22 +25,20 @@
}
});
if (logseq.settings) {
if (logseq.settings.filters){
if (logseq.settings.filters) {
let filters: NodeFilter[] = logseq.settings.filters;
// reset ids to be sequential
filters.forEach((el, index) => {
el.id = index;
return el;
});
$settings.filters = filters
$settings.filters = filters;
} else {
$settings.filters = [];
}
}
logseq.DB.onChanged(() => {
if ($uiVisible) {
store.reload();
Expand All @@ -44,10 +49,10 @@
let metaDown = false;
$: {
if (logseq.settings) {
if (logseq.settings.filters){
if (logseq.settings.filters) {
let filters: Array<NodeFilter> = $settings.filters;
const seen = new Set();
filters = filters.filter(el => {
filters = filters.filter((el) => {
if (el.id === undefined) {
return false;
}
Expand All @@ -56,12 +61,11 @@
return !duplicate;
});
// Doesn't updates without reset if you delete items from list
logseq.updateSettings({filters: null});
logseq.updateSettings({filters: filters});
logseq.updateSettings({ filters: null });
logseq.updateSettings({ filters: filters });
} else {
logseq.updateSettings({filters: []});
logseq.updateSettings({ filters: [] });
}
}
}
Expand Down Expand Up @@ -116,33 +120,25 @@
logseq.hideMainUI();
}
let graphWas: Graph | undefined;
function filteredGraph(
graph: Graph,
filterEnabled: boolean,
search: string | undefined,
filterLength: number
): Graph {
if (graphWas) {
for (const node of graphWas.nodeEntries()) {
if (graph.hasNode(node.node)) {
graph.updateNodeAttribute(node.node, "x", () => node.attributes.x);
graph.updateNodeAttribute(node.node, "y", () => node.attributes.y);
}
}
}
graph.forEachNode((node) => {
graph.setNodeAttribute(node, "hidden", false);
});
if (!filterEnabled || !search) {
graphWas = graph;
return graph;
}
const filterFn = filter(graph, search);
for (const node of graph.nodes()) {
graph.forEachNode((node) => {
if (filterFn(node, filterLength)) {
graph.dropNode(node);
graph.setNodeAttribute(node, "hidden", true);
}
}
graphWas = graph;
});
return graph;
}
</script>
Expand Down
12 changes: 9 additions & 3 deletions src/Sigma.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
graph.forEachNode((node) => {
graph.updateEachNodeAttributes((node, data: Attributes) => {
data.color = grey;
data.hidden = false;
return data;
});
Expand All @@ -224,7 +223,7 @@
) {
attr.color = filter.searchColor;
return attr;
}
}
return attr;
});
});
Expand All @@ -241,7 +240,14 @@
$settings.pathA &&
$settings.pathB
) {
const graph = sigma.getGraph();
$settings.filterLength;
// paht finding algorithm is not working with hidden nodes
const graph = sigma.getGraph().copy();
graph.forEachNode((node, attrs) => {
if (attrs.hidden) {
graph.dropNode(node);
}
});
const results = $settings.directed
? shortestPathDirected(graph, $settings.pathA, $settings.pathB)
: shortestPathUndirected(graph, $settings.pathB, $settings.pathA);
Expand Down
2 changes: 1 addition & 1 deletion src/shortestPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function shortestPathUndirected(
) {
const pathA = findNode(graph, pathAStr);
const pathB = findNode(graph, pathBStr);
if (!pathA && !pathB) {
if (!pathA || !pathB) {
return { nodes: undefined, edges: undefined } as const;
}

Expand Down

0 comments on commit a13cc86

Please sign in to comment.