@@ -15,7 +15,7 @@ import '@vaadin/confirm-dialog';
1515import '@vaadin/progress-bar' ;
1616import MarkdownIt from 'markdown-it' ;
1717import { unsafeHTML } from 'lit/directives/unsafe-html.js' ;
18- import { dialogFooterRenderer , dialogRenderer } from '@vaadin/dialog/lit.js' ;
18+ import { dialogHeaderRenderer , dialogFooterRenderer , dialogRenderer } from '@vaadin/dialog/lit.js' ;
1919import { observeState } from 'lit-element-state' ;
2020import { themeState } from 'theme-state' ;
2121import { notifier } from 'notifier' ;
@@ -83,11 +83,9 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
8383 _filteredActions : { state : true } ,
8484 _selectedWorkspaceItem : { state : true } ,
8585 _changeToWorkspaceItem : { state : true } ,
86- _actionResultContent : { state :true } ,
87- _actionResultDisplay : { state :true } ,
88- _actionResultDisplayType : { state :true } ,
86+ _actionResult : { state : true } ,
8987 _showActionProgress : { state : true } ,
90- _confirmDialogOpened : { state : true }
88+ _confirmDialogOpened : { state : true } ,
9189 } ;
9290
9391 constructor ( ) {
@@ -122,9 +120,8 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
122120 this . _workspaceActions = [ ] ;
123121 this . _filteredActions = this . _workspaceActions ;
124122 this . _clearActionResult ( ) ;
125-
126- this . _loadWorkspaceItems ( ) ;
127123 this . _loadWorkspaceActions ( ) ;
124+ this . _loadWorkspaceItems ( ) ;
128125 }
129126
130127 disconnectedCallback ( ) {
@@ -193,14 +190,16 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
193190 < vaadin-button title ="Copy " slot ="prefix " theme ="icon " aria-label ="Copy " @click ="${ this . _copySelectedWorkspaceItem } ">
194191 < vaadin-icon icon ="font-awesome-solid:copy "> </ vaadin-icon >
195192 </ vaadin-button >
193+
194+
195+ ${ this . _renderActions ( ) }
196+
196197 < qui-ide-link slot ="suffix " title ="Open in IDE " style ="cursor: pointer; "
197198 fileName ="${ this . _selectedWorkspaceItem . path } "
198199 lineNumber ="0 "
199200 noCheck >
200201 < vaadin-icon icon ="font-awesome-solid:up-right-from-square "> </ vaadin-icon >
201202 </ qui-ide-link >
202-
203- ${ this . _renderActions ( ) }
204203
205204 < vaadin-tabs slot ="tabs ">
206205 < vaadin-tab id ="${ this . _selectedWorkspaceItem . path } " title ="${ this . _selectedWorkspaceItem . path } "> ${ this . _selectedWorkspaceItem . name . split ( '/' ) . pop ( ) } </ vaadin-tab >
@@ -217,7 +216,7 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
217216 }
218217
219218 _renderResultSplitView ( ) {
220- if ( this . _actionResultContent && this . _actionResultDisplay === "split" ) {
219+ if ( this . _actionResult && this . _actionResult . content && this . _actionResult . display === "split" ) {
221220 return html `< detail-content style ="width: 50%; ">
222221 < div class ="actionSplitScreen ">
223222 < div class ="actionButtonBar ">
@@ -233,36 +232,54 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
233232 }
234233
235234 _renderResultDialog ( ) {
236- if ( this . _actionResultContent && this . _actionResultDisplay === "dialog" ) {
237- return html `< vaadin-dialog
235+ if ( this . _actionResult && this . _actionResult . content && this . _actionResult . display === "dialog" ) {
236+ return html `< vaadin-dialog style ="min-width=50vw; "
237+ header-title ="${ this . _actionResult ?. name ?? this . _actionResult ?. path } "
238238 resizable
239239 draggable
240- .opened =true
240+ .opened =${ true }
241+ ${ dialogHeaderRenderer (
242+ ( ) => html `
243+ < vaadin-button theme="tertiary " @click ="${ this . _clearActionResult } ">
244+ < vaadin-icon icon ="font-awesome-solid:xmark "> </ vaadin-icon >
245+ </ vaadin-button>
246+ ` ,
247+ [ ]
248+ ) }
241249 ${ dialogRenderer ( this . _renderActionResult , [ ] ) }
250+ ${ dialogFooterRenderer (
251+ ( ) => html `
252+ < vaadin-button theme ="primary " @click ="${ this . _saveActionResult } ">
253+ Save
254+ </ vaadin-button >
255+ < vaadin-button theme ="tertiary " @click ="${ this . _copyActionResult } "> Copy</ vaadin-button >
256+ ` ,
257+ [ ]
258+ ) }
242259 > </ vaadin-dialog> ` ;
243260 }
244261 }
245262
246263 _renderActionResult ( ) {
247- if ( this . _actionResultContent && this . _actionResultDisplayType === "raw" ) {
248- return html `${ this . _actionResultContent } ` ;
249- } else if ( this . _actionResultContent && this . _actionResultDisplayType === "code" ) {
264+ if ( this . _actionResult && this . _actionResult . content && this . _actionResult . displayType === "raw" ) {
265+ return html `${ this . _actionResult . content } ` ;
266+ } else if ( this . _actionResult && this . _actionResult . content && this . _actionResult . displayType === "code" ) {
250267 // TODO: We can not assume the mode is the same as the input
251268 // Maybe return name|content ?
252269 return html `< qui-code-block id ="code " class ='codeBlock '
253- mode ='${ this . _getMode ( this . _selectedWorkspaceItem . name ) } '
270+ mode ='${ this . _getMode ( this . _actionResult ? .name ?? this . _actionResult ?. path ) } '
254271 theme ='${ themeState . theme . name } '
255- .content ='${ this . _actionResultContent } '
272+ .content ='${ this . _actionResult . content } '
256273 showLineNumbers >
257274 </ qui-code-block > ` ;
258- } else if ( this . _actionResultContent && this . _actionResultDisplayType === "markdown" ) {
259- const htmlContent = this . md . render ( this . _actionResultContent ) ;
275+ } else if ( this . _actionResult && this . _actionResult . content && this . _actionResult . displayType === "markdown" ) {
276+ const htmlContent = this . md . render ( this . _actionResult . content ) ;
260277 return html `${ unsafeHTML ( htmlContent ) } ` ;
261- } else if ( this . _actionResultContent && this . _actionResultDisplayType === "html" ) {
262- return html `${ unsafeHTML ( this . _actionResultContent ) } ` ;
263- } else if ( this . _actionResultContent && this . _actionResultDisplayType === "image" ) {
264- let imgurl = `data:image/png;base64,${ this . _actionResultContent } ` ;
265- return html `< img src ="${ imgurl } " alt ="${ this . _selectedWorkspaceItem . name } " style ="max-width: 100%; "/> ` ;
278+ } else if ( this . _actionResult && this . _actionResult . content && this . _actionResult . displayType === "html" ) {
279+ return html `${ unsafeHTML ( this . _actionResult . content ) } ` ;
280+ } else if ( this . _actionResult && this . _actionResult . content && this . _actionResult . displayType === "image" ) {
281+ let imgurl = `data:image/png;base64,${ this . _actionResult . content } ` ;
282+ return html `< img src ="${ imgurl } " alt ="${ this . _actionResult ? .name ?? this . _actionResult ?. path } " style ="max-width: 100%; "/> ` ;
266283 }
267284 }
268285
@@ -366,24 +383,25 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
366383 if ( codeElement ) {
367384 newWorkspaceItemValue = codeElement . getAttribute ( 'value' ) ;
368385 }
369-
370386 this . jsonRpc . executeAction ( { actionId :actionId ,
371387 name :this . _selectedWorkspaceItem . name ,
372388 path :this . _selectedWorkspaceItem . path ,
373389 content :newWorkspaceItemValue ,
374390 type :this . _selectedWorkspaceItem . type } ) . then ( jsonRpcResponse => {
391+
375392 if ( e . detail . value . display === "notification" ) {
376393 notifier . showInfoMessage ( jsonRpcResponse . result . result ) ;
377394 } else if ( e . detail . value . display === "replace" ) {
378- // TODO: This does not take Markdown into context....
379- // TODO: Use result
380- this . _selectedWorkspaceItem . content = jsonRpcResponse . result . result ;
395+ this . _selectedWorkspaceItem . content = jsonRpcResponse . result . result . content ;
381396 this . _selectedWorkspaceItem . type = e . detail . value . displayType ;
382- //this._selectedWorkspaceItem.path =
397+ this . _selectedWorkspaceItem . path = jsonRpcResponse . result . path ;
398+ this . _selectedWorkspaceItem . isDirty = true ;
383399 } else if ( e . detail . value . display !== "nothing" ) {
384- this . _actionResultContent = jsonRpcResponse . result . result ;
385- this . _actionResultDisplay = e . detail . value . display ;
386- this . _actionResultDisplayType = e . detail . value . displayType ;
400+ this . _actionResult = jsonRpcResponse . result . result ;
401+ this . _actionResult . name = this . _actionResult . path ;
402+ this . _actionResult . path = jsonRpcResponse . result . path ;
403+ this . _actionResult . display = e . detail . value . display ;
404+ this . _actionResult . displayType = e . detail . value . displayType ;
387405 }
388406 this . _showActionProgress = false ;
389407 } ) ;
@@ -423,18 +441,9 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
423441 }
424442
425443 _saveSelectedWorkspaceItem ( ) {
426-
427444 let newWorkspaceItemValue = this . _getChangedContent ( ) ;
428445 if ( newWorkspaceItemValue ) {
429- this . jsonRpc . saveWorkspaceItemContent ( { content :newWorkspaceItemValue , path :this . _selectedWorkspaceItem . path } ) . then ( jsonRpcResponse => {
430- if ( jsonRpcResponse . result . success ) {
431- notifier . showInfoMessage ( jsonRpcResponse . result . path + " saved successfully" ) ;
432- this . _selectedWorkspaceItem = { ...this . _selectedWorkspaceItem , content : newWorkspaceItemValue , isDirty : false } ;
433- super . forceRestart ( ) ;
434- } else {
435- notifier . showErrorMessage ( jsonRpcResponse . result . path + " NOT saved. " + jsonRpcResponse . result . errorMessage ) ;
436- }
437- } ) ;
446+ this . _saveContent ( newWorkspaceItemValue , this . _selectedWorkspaceItem . path ) ;
438447 }
439448 }
440449
@@ -445,6 +454,35 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
445454 if ( changedContent ) content = changedContent ;
446455
447456 const path = this . _selectedWorkspaceItem ?. path ;
457+ this . _copyContent ( content , path ) ;
458+ }
459+
460+ _saveActionResult ( ) {
461+ if ( this . _actionResult && this . _actionResult . content ) {
462+ this . _saveContent ( this . _actionResult . content , this . _actionResult . path , false ) ;
463+ }
464+ }
465+
466+ _copyActionResult ( ) {
467+ if ( this . _actionResult && this . _actionResult . content ) {
468+ this . _copyContent ( this . _actionResult . content , this . _actionResult . path ) ;
469+ }
470+ }
471+
472+ _saveContent ( content , path , select = true ) {
473+ this . jsonRpc . saveWorkspaceItemContent ( { content :content , path :path } ) . then ( jsonRpcResponse => {
474+ if ( jsonRpcResponse . result . success ) {
475+ notifier . showInfoMessage ( jsonRpcResponse . result . path + " saved successfully" ) ;
476+ if ( select ) this . _selectedWorkspaceItem = { ...this . _selectedWorkspaceItem , content : content , isDirty : false } ;
477+ //super.forceRestart();
478+ } else {
479+ notifier . showErrorMessage ( jsonRpcResponse . result . path + " NOT saved. " + jsonRpcResponse . result . errorMessage ) ;
480+ }
481+ } ) ;
482+
483+ }
484+
485+ _copyContent ( content , path ) {
448486 if ( ! content ) {
449487 notifier . showWarningMessage ( path + " has no content" ) ;
450488 return ;
@@ -469,10 +507,7 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
469507 }
470508
471509 _clearActionResult ( ) {
472- this . _actionResultContent = null ;
473- this . _actionResultDisplay = null ;
474- this . _actionResultDisplayType = null ;
475- this . _showActionProgress = false ;
510+ this . _actionResult = null ;
476511 }
477512
478513 _selectWorkspaceItem ( workspaceItem ) {
@@ -501,9 +536,11 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
501536 this . _filterActions ( workspaceItem . name ) ;
502537
503538 this . _clearActionResult ( ) ;
539+ this . _showActionProgress = false ;
504540 }
505541
506542 shouldConfirmAwayNavigation ( ) {
543+ if ( this . _selectedWorkspaceItem . isDirty ) return true ;
507544 let changedContent = this . _getChangedContent ( ) ;
508545 if ( changedContent ) {
509546 return true ;
@@ -513,11 +550,15 @@ export class QwcWorkspace extends observeState(QwcHotReloadElement) {
513550
514551 _getChangedContent ( ) {
515552 if ( this . _selectedWorkspaceItem . content ) {
516- let codeElement = this . shadowRoot . getElementById ( 'code' ) ;
517- if ( codeElement ) {
518- let newWorkspaceItemValue = codeElement . getAttribute ( 'value' ) ;
519- if ( newWorkspaceItemValue !== this . _selectedWorkspaceItem . content ) {
520- return newWorkspaceItemValue ;
553+ if ( this . _selectedWorkspaceItem . isDirty ) {
554+ return this . _selectedWorkspaceItem . content ;
555+ } else {
556+ let codeElement = this . shadowRoot . getElementById ( 'code' ) ;
557+ if ( codeElement ) {
558+ let newWorkspaceItemValue = codeElement . getAttribute ( 'value' ) ;
559+ if ( newWorkspaceItemValue !== this . _selectedWorkspaceItem . content ) {
560+ return newWorkspaceItemValue ;
561+ }
521562 }
522563 }
523564 }
0 commit comments