|
1 | | -import { Editor, Modal, Setting } from "obsidian"; |
2 | | -import { |
3 | | - DEFAULT_PLOT_OPTIONS, |
4 | | -} from "../common/defaults"; |
| 1 | +import { Editor, Modal } from "obsidian"; |
5 | 2 | import type { PlotOptions } from "../common/types"; |
6 | 3 | import type ObsidianFunctionPlot from "../main"; |
7 | | -import FunctionsList from "../components/CreatePlot/FunctionsList.svelte"; |
8 | | -import functionPlot from "function-plot"; |
| 4 | +import PlotModal from "../components/PlotModal/PlotModal.svelte"; |
| 5 | +import { DEFAULT_PLOT_INPUTS } from "../common/defaults"; |
| 6 | +import { insertPlot } from "../common/utils"; |
9 | 7 |
|
10 | 8 | export default class CreatePlotModal extends Modal { |
11 | | - options: PlotOptions; |
12 | 9 | plugin: ObsidianFunctionPlot; |
13 | 10 | editor: Editor; |
14 | | - rendertarget: HTMLElement; |
15 | 11 |
|
16 | 12 | constructor(plugin: ObsidianFunctionPlot, editor: Editor) { |
17 | 13 | super(plugin.app); |
18 | 14 | this.plugin = plugin; |
19 | 15 | this.editor = editor; |
20 | 16 | } |
21 | 17 |
|
22 | | - reloadPreview() { |
23 | | - if (!this.rendertarget) return; |
24 | | - console.log(this.options) |
25 | | - functionPlot( |
26 | | - Object.assign({}, this.options, {target: this.rendertarget}) |
27 | | - ) |
28 | | - } |
29 | | - |
30 | 18 | async onOpen() { |
31 | | - // fix modal size |
32 | | - document |
33 | | - .getElementsByClassName("modal")[0] |
34 | | - .setAttribute( |
35 | | - "style", |
36 | | - "width: var(--max-modal-width); height: var(--max-modal-height);" |
37 | | - ); |
38 | | - |
39 | | - this.options = JSON.parse(JSON.stringify(DEFAULT_PLOT_OPTIONS)); // deepcopy to avoid side effects |
40 | | - |
41 | | - const { contentEl } = this; |
42 | | - contentEl.empty(); |
43 | | - |
44 | | - // Header |
45 | | - contentEl.createEl("h1", { text: "Create a plot" }); |
46 | | - |
47 | | - const flex = contentEl.createDiv({ |
48 | | - attr: { style: "display: flex; align-items: center" }, |
49 | | - }); |
50 | | - |
51 | | - const settings = flex.createDiv(); |
52 | | - const preview = flex.createDiv({ attr: { style: "padding: 1em" } }); |
53 | | - this.rendertarget = preview.createDiv() |
54 | | - |
55 | | - preview.createEl("p", { |
56 | | - text: "Preview - Zoom is disabled while in preview", |
57 | | - attr: { |
58 | | - style: "margin: 0 3em; font-size: 0.8em; color: var(--text-faint)", |
| 19 | + new PlotModal({ |
| 20 | + target: this.contentEl, |
| 21 | + props: { |
| 22 | + options: Object.assign( |
| 23 | + JSON.parse(JSON.stringify(DEFAULT_PLOT_INPUTS)), |
| 24 | + { renderer: this.plugin.settings.defaultRenderer } // assign default renderer as initial dropdown state |
| 25 | + ), |
| 26 | + plugin: this.plugin, |
| 27 | + submit: (options: PlotOptions) => { |
| 28 | + insertPlot(this.plugin, this.editor, options); |
| 29 | + this.close(); |
| 30 | + }, |
59 | 31 | }, |
60 | 32 | }); |
61 | | - |
62 | | - new Setting(settings).setName("Title").addText((text) => { |
63 | | - text.onChange(async (value) => { |
64 | | - this.options.title = value; |
65 | | - this.reloadPreview(); |
66 | | - }); |
67 | | - }); |
68 | | - |
69 | | - new Setting(settings).setName("Label X").addText((text) => { |
70 | | - text.onChange(async (value) => { |
71 | | - this.options.xAxis.label = value; |
72 | | - this.reloadPreview(); |
73 | | - }); |
74 | | - }); |
75 | | - new Setting(settings).setName("Label Y").addText((text) => { |
76 | | - text.onChange(async (value) => { |
77 | | - this.options.yAxis.label = value; |
78 | | - this.reloadPreview(); |
79 | | - }); |
80 | | - }); |
81 | | - |
82 | | - /*const placeholders = ["X min", "X max", "Y min", "Y max"]; |
83 | | -
|
84 | | - const bounds = new Setting(settings).setName("Bounds"); |
85 | | -
|
86 | | - placeholders.forEach((placeholder, i) => { |
87 | | - bounds.addText((text) => { |
88 | | - text |
89 | | - .setPlaceholder(placeholder) |
90 | | - .onChange((value) => { |
91 | | - if (value && !isNaN(+value)) { |
92 | | - this.options.bounds[i] = +value; |
93 | | - this.reloadPreview(); |
94 | | - } else { |
95 | | - this.options.bounds[i] = DEFAULT_PLOT_OPTIONS.bounds[i]; |
96 | | - this.reloadPreview(); |
97 | | - } |
98 | | - }) |
99 | | - .inputEl.setAttribute("style", "width: 4em"); |
100 | | - }); |
101 | | - });*/ |
102 | | - |
103 | | - new Setting(settings).setName("Disable Zoom").addToggle((com) => { |
104 | | - com.setValue(this.options.disableZoom); |
105 | | - com.onChange(async (value) => { |
106 | | - this.options.disableZoom = value; |
107 | | - this.reloadPreview(); |
108 | | - }); |
109 | | - }); |
110 | | - |
111 | | - new Setting(settings).setName("Enable Grid").addToggle((com) => { |
112 | | - com.setValue(this.options.grid); |
113 | | - com.onChange(async (value) => { |
114 | | - this.options.grid = value; |
115 | | - this.reloadPreview(); |
116 | | - }); |
117 | | - }); |
118 | | - |
119 | | - const dataSetting = new Setting(settings) |
120 | | - .setName("Data") |
121 | | - dataSetting.settingEl.setAttribute("style", "display: block"); |
122 | | - |
123 | | - new FunctionsList({ |
124 | | - target: dataSetting.controlEl, |
125 | | - props: { |
126 | | - options: this.options, |
127 | | - reloadPreview: this.reloadPreview.bind(this), |
128 | | - } |
129 | | - }) |
130 | | - |
131 | | - new Setting(contentEl) |
132 | | - /*.addDropdown((com) => { |
133 | | - com |
134 | | - .addOptions(rendererOptions) |
135 | | - .setValue(this.plugin.settings.defaultRenderer) |
136 | | - .onChange((value: rendererType) => { |
137 | | - this.renderer = value; |
138 | | - }); |
139 | | - })*/ |
140 | | - .addButton((btn) => { |
141 | | - btn |
142 | | - .setButtonText("Plot") |
143 | | - .setCta() |
144 | | - .onClick(async () => { |
145 | | - // await this.handleFinalPlotCreate(this.options); |
146 | | - }); |
147 | | - }); |
148 | | - } |
149 | | - |
150 | | - /* |
151 | | - async handleFinalPlotCreate(options: PlotOptions) { |
152 | | - // render and insert chosen plot using renderer |
153 | | - switch ("interactive") { |
154 | | - case "interactive": |
155 | | - renderPlotAsInteractive(this.plugin, this.editor, options); |
156 | | - break; |
157 | | - /* |
158 | | - case "image": |
159 | | - await renderPlotAsImage(this.plugin, this.editor, options); |
160 | | - break; *//* |
161 | | - } |
162 | | -
|
163 | | - this.close(); |
164 | | - } |
165 | | - */ |
166 | | - |
167 | | - onClose() { |
168 | | - this.contentEl.empty(); |
169 | 33 | } |
170 | 34 | } |
0 commit comments