1- import type { FunctionPlotDatum } from "function-plot/dist/types" ;
21import { Editor , Modal , Setting } from "obsidian" ;
32import {
4- DEFAULT_FUNCTION_OPTIONS ,
53 DEFAULT_PLOT_OPTIONS ,
6- FUNCTION_CASES ,
74} from "../common/defaults" ;
8- import type { PlotOptions , rendererType , Line } from "../common/types" ;
5+ import type { PlotOptions } from "../common/types" ;
96import type ObsidianFunctionPlot from "../main" ;
10- import { createPlot , renderPlotAsInteractive } from "../common/utils " ;
11- import type { Chart } from "function-plot" ;
7+ import FunctionsList from "../components/CreatePlot/FunctionsList.svelte " ;
8+ import functionPlot from "function-plot" ;
129
1310export default class CreatePlotModal extends Modal {
1411 options : PlotOptions ;
1512 plugin : ObsidianFunctionPlot ;
1613 editor : Editor ;
17- plot : Chart ;
18- renderer : rendererType ;
14+ rendertarget : HTMLElement ;
1915
2016 constructor ( plugin : ObsidianFunctionPlot , editor : Editor ) {
2117 super ( plugin . app ) ;
2218 this . plugin = plugin ;
2319 this . editor = editor ;
24- this . renderer = this . plugin . settings . defaultRenderer ;
2520 }
2621
27- /**
28- * Reload the preview using internal functions. Zooming doesn't work here.
29- * @returns A Promise
30- */
3122 reloadPreview ( ) {
32- if ( ! this . plot ) return ;
33- // update values
34- this . plot . options . title = this . options . title ;
35- this . plot . options . xAxis . label = this . options . xLabel ;
36- this . plot . options . yAxis . label = this . options . yLabel ;
37- this . plot . options . xAxis . domain = this . options . bounds . slice ( 0 , 2 ) ;
38- this . plot . options . yAxis . domain = this . options . bounds . slice ( 2 , 4 ) ;
39- this . plot . options . grid = this . options . grid ;
40- this . plot . options . data = this . options . functions . map (
41- ( line ) : FunctionPlotDatum => {
42- // use polyline by default
43- const lineProperties : Line = { graphType : "polyline" } ;
44-
45- line . split ( "@" ) . forEach ( ( property ) => {
46- const tup = property . split ( "=" ) ;
47- const value = tup [ 1 ] . trim ( ) ;
48- lineProperties [ tup [ 0 ] . trim ( ) ] = value . startsWith ( "[" )
49- ? JSON . parse ( value )
50- : value ;
51- } ) ;
52-
53- return lineProperties ;
54- }
55- ) ;
56- // redirect errors within function-plot to debug
57- try {
58- this . plot . build ( ) ;
59- } catch ( e ) {
60- console . debug ( e ) ;
61- }
62- }
63-
64- computeStates ( cases , options ) {
65- const states = [ ] ;
66- for ( const [ test ] of cases ) {
67- states . push ( test ( options ) ) ;
68- }
69- return states ;
23+ if ( ! this . rendertarget ) return ;
24+ console . log ( this . options )
25+ functionPlot (
26+ Object . assign ( { } , this . options , { target : this . rendertarget } )
27+ )
7028 }
7129
7230 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+
7339 this . options = JSON . parse ( JSON . stringify ( DEFAULT_PLOT_OPTIONS ) ) ; // deepcopy to avoid side effects
7440
7541 const { contentEl } = this ;
7642 contentEl . empty ( ) ;
7743
7844 // Header
79- contentEl . createEl ( "h1" , { text : "Plot a function " } ) ;
45+ contentEl . createEl ( "h1" , { text : "Create a plot " } ) ;
8046
8147 const flex = contentEl . createDiv ( {
8248 attr : { style : "display: flex; align-items: center" } ,
8349 } ) ;
8450
8551 const settings = flex . createDiv ( ) ;
8652 const preview = flex . createDiv ( { attr : { style : "padding: 1em" } } ) ;
87- this . plot = await createPlot (
88- Object . assign ( { } , this . options , { disableZoom : true } ) ,
89- preview . createDiv ( ) ,
90- this . plugin
91- ) ;
53+ this . rendertarget = preview . createDiv ( )
54+
9255 preview . createEl ( "p" , {
9356 text : "Preview - Zoom is disabled while in preview" ,
9457 attr : {
@@ -105,18 +68,18 @@ export default class CreatePlotModal extends Modal {
10568
10669 new Setting ( settings ) . setName ( "Label X" ) . addText ( ( text ) => {
10770 text . onChange ( async ( value ) => {
108- this . options . xLabel = value ;
71+ this . options . xAxis . label = value ;
10972 this . reloadPreview ( ) ;
11073 } ) ;
11174 } ) ;
11275 new Setting ( settings ) . setName ( "Label Y" ) . addText ( ( text ) => {
11376 text . onChange ( async ( value ) => {
114- this . options . yLabel = value ;
77+ this . options . yAxis . label = value ;
11578 this . reloadPreview ( ) ;
11679 } ) ;
11780 } ) ;
11881
119- const placeholders = [ "X min" , "X max" , "Y min" , "Y max" ] ;
82+ /* const placeholders = ["X min", "X max", "Y min", "Y max"];
12083
12184 const bounds = new Setting(settings).setName("Bounds");
12285
@@ -129,16 +92,13 @@ export default class CreatePlotModal extends Modal {
12992 this.options.bounds[i] = +value;
13093 this.reloadPreview();
13194 } else {
132- console . log (
133- `resetting ${ i } to default ${ DEFAULT_PLOT_OPTIONS . bounds [ i ] } `
134- ) ;
13595 this.options.bounds[i] = DEFAULT_PLOT_OPTIONS.bounds[i];
13696 this.reloadPreview();
13797 }
13898 })
139- . inputEl . classList . add ( "function-plot-numberinput ") ;
99+ .inputEl.setAttribute("style", "width: 4em ");
140100 });
141- } ) ;
101+ });*/
142102
143103 new Setting ( settings ) . setName ( "Disable Zoom" ) . addToggle ( ( com ) => {
144104 com . setValue ( this . options . disableZoom ) ;
@@ -156,48 +116,17 @@ export default class CreatePlotModal extends Modal {
156116 } ) ;
157117 } ) ;
158118
159- const functionsSetting = new Setting ( settings )
160- . setName ( "Functions" )
161- . setDesc ( "Functions to plot." ) ;
162- functionsSetting . settingEl . setAttribute ( "style" , "display: block" ) ;
163- const functionsControlEl = functionsSetting . controlEl ;
164- functionsControlEl . classList . value = "function-plot-functions-container" ;
165- const functionsList = functionsControlEl . createDiv ( {
166- attr : { class : "function-plot-functions-list" } ,
167- } ) ;
168-
169- functionsControlEl . createDiv (
170- { attr : { class : "function-plot-functions-add" } } ,
171- ( el ) => {
172- new Setting ( el ) . addButton ( ( btn ) => {
173- btn
174- . setButtonText ( "Add function" )
175- . setIcon ( "plus" )
176- . onClick ( async ( ) => {
177- const id = Math . random ( ) . toString ( 36 ) . substring ( 2 , 9 ) ;
178- const options = Object . assign (
179- JSON . parse ( JSON . stringify ( DEFAULT_FUNCTION_OPTIONS ) ) ,
180- { id }
181- ) ;
182- const prevStates = this . computeStates ( FUNCTION_CASES , options ) ;
183-
184- new Setting ( functionsList ) . addDropdown ( ( com ) => {
185- com
186- . addOptions ( {
187- linear : "linear" ,
188- vector : "vector" ,
189- polar : "polar" ,
190- points : "points" ,
191- } )
192- . setValue ( options . fnType )
193- . onChange ( ( value ) => {
194- options . fnType = value ;
195- } ) ;
196- } ) ;
197- } ) ;
198- } ) ;
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 ) ,
199128 }
200- ) ;
129+ } )
201130
202131 new Setting ( contentEl )
203132 /*.addDropdown((com) => {
@@ -213,29 +142,29 @@ export default class CreatePlotModal extends Modal {
213142 . setButtonText ( "Plot" )
214143 . setCta ( )
215144 . onClick ( async ( ) => {
216- await this . handleFinalPlotCreate ( this . options ) ;
145+ // await this.handleFinalPlotCreate(this.options);
217146 } ) ;
218147 } ) ;
219148 }
220149
150+ /*
221151 async handleFinalPlotCreate(options: PlotOptions) {
222152 // render and insert chosen plot using renderer
223- switch ( this . renderer ) {
153+ switch ("interactive" ) {
224154 case "interactive":
225155 renderPlotAsInteractive(this.plugin, this.editor, options);
226156 break;
227157 /*
228158 case "image":
229159 await renderPlotAsImage(this.plugin, this.editor, options);
230- break; */
160+ break; */ /*
231161 }
232162
233163 this.close();
234164 }
165+ */
235166
236167 onClose ( ) {
237- const { contentEl } = this ;
238- contentEl . empty ( ) ;
239- this . plot = null ;
168+ this . contentEl . empty ( ) ;
240169 }
241170}
0 commit comments