Skip to content

Commit 59a4c9b

Browse files
committed
- Improved function name regex rule
- made the datums function name be the regexed result - Some code clean up
1 parent 56bf6e1 commit 59a4c9b

6 files changed

Lines changed: 47 additions & 37 deletions

File tree

src/app/CreatePlotModal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class CreatePlotModal extends Modal {
99
plugin: ObsidianFunctionPlot;
1010
editor: Editor;
1111
options: PlotInputs;
12+
// eslint-disable-next-line no-unused-vars
1213
onSubmit: (options: PlotInputs, renderer: rendererType) => void;
1314

1415
constructor(

src/common/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function parseLaTeX(
3333
? latex
3434
: latex
3535
.replace(plugin.settings.decimalSeparator, ".")
36-
.replace(/\w+\(x\)=/, "") // Replace any function name(x)= pattern
36+
.replace(/\w+\(x\) *=/, "") // Replace any function name(x)= pattern
3737
.replace(/\\frac\{([^{}]+)\}\{([^{}]+)\}/g, "($1)/($2)")
3838
.replace(/\\cdot/g, "*")
3939
.replace(/\^{([^}]*)}/g, "^($1)")
@@ -160,7 +160,7 @@ export function toFunctionPlotOptions(
160160
grid: options.grid ?? undefined,
161161
disableZoom: options.disableZoom ?? undefined,
162162
tip: {
163-
renderer: (x: number, y: number, index: number) => {
163+
renderer: (x: number, y: number) => {
164164
return options.tip.renderer === undefined || options.tip.renderer === ""
165165
? "(" + x.toFixed(2).toString() + "," + y.toFixed(2).toString() + ")"
166166
: options.tip.renderer

src/components/Plot/Constant.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
if (!target) return;
2626
const rect = target.getBoundingClientRect();
2727
mousePos = { x: rect.right, y: rect.bottom };
28-
console.log(mousePos);
2928
}
3029
</script>
3130

src/components/Plot/Plot.svelte

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,15 @@
66
77
import { FunctionPlot } from "../../fnplot";
88
import { onMount } from "svelte";
9-
import PlotModal from "../PlotModal/PlotModal.svelte";
109
import CreatePlotModal from "../../app/CreatePlotModal";
11-
import { timeHours } from "d3";
12-
import { insertPlot } from "../../common/utils";
1310
1411
export let options: PlotInputs,
1512
plugin: ObsidianFunctionPlot,
1613
showConstantsSettings = false;
1714
1815
let plotContainer: HTMLElement;
1916
let plot: FunctionPlot;
20-
let prevGraphType = new Array();
21-
22-
onMount(() => {
23-
createNewPlot();
24-
});
25-
// $: {
26-
// const scope = (
27-
// Object.entries(options.constants) as [string, ConstantInputs][]
28-
// ).reduce((scope_, [key, val]) => {
29-
// scope_[key] = val.value;
30-
// return scope_;
31-
// }, {});
32-
// options.data.forEach((datum) => {
33-
// datum.scope = scope;
34-
// });
35-
// }
17+
let prevGraphType = [];
3618
3719
function createNewPlot() {
3820
// First destroy any existing plot completely
@@ -50,6 +32,21 @@
5032
plot.render();
5133
}
5234
35+
onMount(() => {
36+
createNewPlot();
37+
});
38+
// $: {
39+
// const scope = (
40+
// Object.entries(options.constants) as [string, ConstantInputs][]
41+
// ).reduce((scope_, [key, val]) => {
42+
// scope_[key] = val.value;
43+
// return scope_;
44+
// }, {});
45+
// options.data.forEach((datum) => {
46+
// datum.scope = scope;
47+
// });
48+
// }
49+
5350
$: {
5451
if (options?.data) {
5552
for (let i = 0; i < options.data.length; i++) {

src/components/PlotModal/Function.svelte

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
mousePos = { x: rect.right, y: rect.bottom };
2727
}
2828
29-
function handlePointsChange(e: MouseEvent) {
29+
function handlePointsChange() {
3030
points += 1;
3131
datum.points.push([0, 0]);
3232
}
@@ -35,6 +35,17 @@
3535
points -= 1;
3636
datum.points.remove(datum.points[i]);
3737
}
38+
39+
$: {
40+
if (datum.fnType === "linear" || datum.fnType === "polar") {
41+
let fnString = datum.fnType === "linear" ? datum.fn : datum.r;
42+
let match = fnString?.match(/\w+\(x\) *=/);
43+
44+
if (match) {
45+
datum.name = match[0].replaceAll("=", "").replaceAll("(x)", "").trim();
46+
}
47+
}
48+
}
3849
</script>
3950

4051
<div class="functionplot-item-data">
@@ -72,7 +83,7 @@
7283

7384
<IconWrapper
7485
style="align-self: center; transform: translateY(0.2em);"
75-
on:click={(e) => removePoint(i)}
86+
on:click={() => removePoint(i)}
7687
>
7788
<Delete size="1.1em" />
7889
</IconWrapper>
@@ -137,7 +148,7 @@
137148
<label for="skip-tip">Skip tip</label>
138149
<Switch id="skip-tip" bind:checked={datum.skipTip} />
139150
{/if}
140-
{#if datum.fnType == "linear"}
151+
{#if datum.fnType === "linear"}
141152
<label for="derivative">Derivative</label>
142153
<TextInput id="derivative" bind:value={datum.derivative.fn} />
143154
{/if}

src/components/PlotModal/PlotModal.svelte

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,18 @@
5757
import { onMount } from "svelte";
5858
import OptionsFloater from "../Primitives/OptionsFloater.svelte";
5959
60-
onMount(async () => {
60+
// create a new function item
61+
function newDataItem() {
62+
options.data = [
63+
...options.data,
64+
Object.assign(JSON.parse(JSON.stringify(DEFAULT_FUNCTION_INPUTS)), {
65+
id: Math.random().toString(36).substring(2, 9),
66+
color: hueToHexRGB(hues.next().value as number),
67+
}) as FunctionInputs,
68+
];
69+
}
70+
71+
onMount(() => {
6172
// Get the currently selected text and set it as the first function
6273
const activeLeaf = plugin.app.workspace.getActiveViewOfType(MarkdownView);
6374
let selectedText = "";
@@ -85,17 +96,6 @@
8596
mousePos = { x: rect.right, y: rect.bottom };
8697
}
8798
88-
// create a new function item
89-
function newDataItem() {
90-
options.data = [
91-
...options.data,
92-
Object.assign(JSON.parse(JSON.stringify(DEFAULT_FUNCTION_INPUTS)), {
93-
id: Math.random().toString(36).substring(2, 9),
94-
color: hueToHexRGB(hues.next().value as number),
95-
}) as FunctionInputs,
96-
];
97-
}
98-
9999
$: {
100100
const constants = options.data.reduce((acc: string[], datum) => {
101101
const toParse: string[] = [],
@@ -109,6 +109,8 @@
109109
} else if (datum.fnType === "polar") {
110110
toParse.push(datum.r ?? "");
111111
ignored.push("theta");
112+
ignored.push("^");
113+
ignored.push("theta^");
112114
} // vector doesn't support constants currently
113115
114116
toParse.forEach((fn: string) => {

0 commit comments

Comments
 (0)