@@ -5,10 +5,6 @@ require('../loading/css/jquery-loading.css');
55import Editor from '@toast-ui/editor'
66import codeSyntaxHighlight from '@toast-ui/editor-plugin-code-syntax-highlight'
77import colorSyntax from '@toast-ui/editor-plugin-color-syntax'
8- import tableMergedCell from '@toast-ui/editor-plugin-table-merged-cell'
9- import '@toast-ui/editor/dist/toastui-editor.css' ;
10- import 'tui-color-picker/dist/tui-color-picker.css' ;
11- import '@toast-ui/editor-plugin-color-syntax/dist/toastui-editor-plugin-color-syntax.css' ;
128import '../loading/js/jquery-loading' ;
139import 'jquery' ;
1410import 'datatables.net' ;
@@ -19,18 +15,15 @@ import 'jquery-ui';
1915import 'jquery-confirm' ;
2016import '../scripts/jquery.autocomplete.min' ;
2117import 'select2' ;
22- import { marked } from 'marked' ;
2318import Chart from 'chart.js/auto' ;
24- import TurndownService from 'turndown'
25- let html2md = new TurndownService ( )
19+ import { FactionEditor } from '../utils/editor' ;
2620
2721
2822
2923global . _token = $ ( "#_token" ) [ 0 ] . value ;
30- let editors = {
31- risk : { } ,
32- summary : { }
33- } ;
24+ let assessmentId = $ ( "#assessmentId" ) [ 0 ] . value
25+ console . log ( assessmentId ) ;
26+ let editors = new FactionEditor ( assessmentId ) ;
3427let initialHTML = { }
3528
3629
@@ -66,9 +59,6 @@ function alertMessage(resp, success) {
6659 global . _token = resp . token ;
6760}
6861
69- function getEditorText ( name ) {
70- return editors [ name ] . getHTML ( )
71- }
7262function showLoading ( com ) {
7363 $ ( com ) . loading ( { overlay : true , base : 0.3 } ) ;
7464}
@@ -114,8 +104,8 @@ function saveAllEditors(showLoadingScreen = false) {
114104 if ( showLoadingScreen ) {
115105 showLoading ( ".content" ) ;
116106 }
117- let risk = getEditorText ( 'risk' ) ;
118- let sum = getEditorText ( 'summary' ) ;
107+ let risk = editors . getEditorText ( 'risk' ) ;
108+ let sum = editors . getEditorText ( 'summary' ) ;
119109 let data = "riskAnalysis=" + encodeURIComponent ( risk ) ;
120110 data += "&summary=" + encodeURIComponent ( sum ) ;
121111 data += "&id=app" + $ ( "#appid" ) [ 0 ] . value
@@ -139,7 +129,7 @@ function saveAllEditors(showLoadingScreen = false) {
139129
140130function saveEditor ( type ) {
141131
142- let edits = getEditorText ( type ) ;
132+ let edits = editors . getEditorText ( type ) ;
143133 let name = "" ;
144134 let data = "" ;
145135 if ( type == "risk" ) {
@@ -178,95 +168,23 @@ function queueSave(type) {
178168 }
179169 } ) ;
180170
181- }
182- function b64DecodeUnicode ( str ) {
183- str = decodeURIComponent ( str ) ;
184- return decodeURIComponent ( Array . prototype . map . call ( atob ( str ) , function ( c ) {
185- return '%' + ( '00' + c . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 2 )
186- } ) . join ( '' ) ) ;
187- }
188- function setEditorContents ( contents , editor , isEncoded ) {
189- if ( isEncoded ) {
190- contents = b64DecodeUnicode ( contents )
191- }
192- console . log ( contents )
193- editors [ editor ] . setHTML ( contents , false ) ;
194- editors [ editor ] . moveCursorToStart ( false ) ;
195- }
196- function entityDecode ( encoded ) {
197- let textArea = document . createElement ( "textarea" ) ;
198- textArea . innerHTML = encoded ;
199- return textArea . innerText ;
200-
201171}
202172
203- function createEditor ( id ) {
204- initialHTML [ id ] = entityDecode ( $ ( `#${ id } ` ) . html ( ) ) ;
205- $ ( `#${ id } ` ) . html ( "" ) ;
206- editors [ id ] = new Editor ( {
207- el : document . querySelector ( `#${ id } ` ) ,
208- previewStyle : 'vertical' ,
209- height : 'auto' ,
210- autofocus : false ,
211- height : '560px' ,
212- plugins : [ colorSyntax , tableMergedCell ] ,
213- hooks : {
214- addImageBlobHook : async ( blob , callback , source ) => {
215- const encodedImage = await imageToURL ( blob )
216- let data = "encodedImage=" + encodeURIComponent ( encodedImage ) ;
217- data += "&assessmentId=" + $ ( "#appid" ) [ 0 ] . value ;
218- $ . post ( "UploadImage" , data ) . done ( function ( resp ) {
219- let uuid = resp . message ;
220- callback ( "getImage?id=" + uuid ) ;
221- } ) ;
222-
223- }
224- }
225- } ) ;
226- editors [ id ] . hide ( ) ;
227- editors [ id ] . setHTML ( initialHTML [ id ] , false ) ;
228- initialHTML [ id ] = editors [ id ] . getHTML ( ) ;
229- editors [ id ] . show ( ) ;
230- editors [ id ] . on ( 'change' , function ( ) {
231- if ( document . getElementById ( `${ id } _header` ) . innerHTML == "" ) {
232- queueSave ( id ) ;
233- }
234- } ) ;
235-
236- /// This is a hack becuase toastui does not have inital undo history set correctly
237- /// https://github.com/nhn/tui.editor/issues/3195
238- editors [ id ] . on ( 'keydown' , function ( a , e ) {
239- const html = editors [ id ] . getHTML ( )
240- if ( ( e . ctrlKey || e . metaKey ) && e . key == 'z' && html == initialHTML [ id ] ) {
241- e . preventDefault ( ) ;
242- throw new Error ( "Prevent Undo" ) ;
243- }
244- } )
245-
246- }
247173$ ( function ( ) {
174+
248175 global . _token = $ ( "#_token" ) [ 0 ] . value ;
249- createEditor ( "summary" )
250- createEditor ( "risk" )
251- let initialNotes = entityDecode ( $ ( "#engagmentnotes" ) . html ( ) ) ;
252- $ ( "#engagmentnotes" ) . html ( "" ) ;
253- editors . engagenotes = new Editor ( {
254- el : document . querySelector ( '#engagmentnotes' ) ,
255- toolbarItems :[ ] ,
256- previewStyle : 'vertical' ,
257- autofocus : false ,
258- viewer : true ,
259- height : '520px' ,
260- initialEditType : 'wysiwyg'
261- } ) ;
262- editors . engagenotes . setHTML ( initialNotes , false ) ;
263- editors . engagenotes . on ( 'keydown' , function ( t , e ) {
264- if ( ! ( ( e . ctrlKey || e . metaKey ) && e . key == 'c' ) ) {
265- e . preventDefault ( ) ;
266- throw new Error ( "Prevent Edit" ) ;
267- }
268-
176+ editors . createEditor ( "summary" , true , ( ) => {
177+ if ( document . getElementById ( `summary_header` ) . innerHTML == "" ) {
178+ queueSave ( "summary" ) ;
179+ }
180+ } ) ;
181+ editors . createEditor ( "risk" , true , ( ) => {
182+ if ( document . getElementById ( `risk_header` ) . innerHTML == "" ) {
183+ queueSave ( "risk" ) ;
184+ }
269185 } ) ;
186+ let initialNotes = editors . entityDecode ( $ ( "#engagementnotes" ) . html ( ) ) ;
187+ editors . createReadOnly ( "engagementnotes" , initialNotes ) ;
270188 let errorMessageShown = false ;
271189 setInterval ( ( ) => {
272190 $ . get ( "summary/check/locks" ) . done ( ( resp ) => {
@@ -296,7 +214,7 @@ $(function() {
296214 $ ( "#" + type ) . addClass ( "disabled" )
297215
298216 document . getElementById ( `${ type } _header` ) . innerHTML = `<i class="lockUser">Editing by ${ resp [ type ] . lockBy } ${ resp [ type ] . lockAt } </i>`
299- setEditorContents ( resp [ type ] . updatedText , type , true ) ;
217+ editors . setEditorContents ( type , resp [ type ] . updatedText , true ) ;
300218 } else {
301219 if ( document . getElementById ( `${ type } _header` ) . innerHTML . indexOf ( "*" ) == - 1 ) {
302220 document . getElementById ( `${ type } _header` ) . innerHTML = "" ;
@@ -564,7 +482,7 @@ $(function() {
564482 $ ( ".saveTemp" ) . click ( function ( ) {
565483 var id = $ ( this ) . attr ( "for" ) ;
566484 var data = "term=" + $ ( "#" + id ) . val ( ) ;
567- data += "&summary=" + encodeURIComponent ( getEditorText ( $ ( "#" + id ) . attr ( "for" ) ) ) ;
485+ data += "&summary=" + encodeURIComponent ( editors . getEditorText ( $ ( "#" + id ) . attr ( "for" ) ) ) ;
568486 if ( $ ( "#" + id ) . attr ( "for" ) == "step_description" )
569487 data += "&exploit=true" ;
570488 data += "&_token=" + global . _token ;
@@ -606,7 +524,7 @@ $(function() {
606524 selectedText = $ ( "#tempName" ) . val ( ) ;
607525 }
608526 let data = `term=${ selectedText . trim ( ) } `
609- data += "&summary=" + encodeURIComponent ( getEditorText ( type ) ) ;
527+ data += "&summary=" + encodeURIComponent ( editors . getEditorText ( type ) ) ;
610528 data += `&type=${ type } ` ;
611529 data += "&active=true"
612530 data += "&_token=" + global . _token ;
@@ -711,8 +629,8 @@ $(function() {
711629 await $ . get ( 'tempSearchDetail?tmpId=' + value )
712630 . done ( function ( data ) {
713631 let template = data . templates [ 0 ] . text ;
714- let text = getEditorText ( type ) + "\n\n" + template ;
715- setEditorContents ( text , type , false ) ;
632+ let text = editors . getEditorText ( type ) + "\n\n" + template ;
633+ editors . setEditorContents ( type , text , false ) ;
716634 } ) ;
717635
718636 }
@@ -738,21 +656,21 @@ $(function() {
738656 overWrite : {
739657 text : "OverWrite" ,
740658 action : function ( ) {
741- setEditorContents ( text , type , false ) ;
659+ editors . setEditorContents ( type , text , false ) ;
742660 }
743661 } ,
744662 prepend : {
745663 text : "Prepend" ,
746664 action : function ( ) {
747- text = text + "\n\n" + getEditorText ( type ) ;
748- setEditorContents ( text , type , false ) ;
665+ text = text + "\n\n" + editors . getEditorText ( type ) ;
666+ editors . setEditorContents ( type , text , false ) ;
749667 }
750668 } ,
751669 append : {
752670 text : "Append" ,
753671 action : function ( ) {
754- text = getEditorText ( type ) + "\n\n" + text ;
755- setEditorContents ( text , type , false ) ;
672+ text = editors . getEditorText ( type ) + "\n\n" + text ;
673+ editors . setEditorContents ( type , text , false ) ;
756674 }
757675 } ,
758676 cancel : function ( ) {
@@ -788,7 +706,7 @@ $(function() {
788706 ) ;
789707 } ,
790708 onSelect : function ( e , term , item ) {
791- var s = getEditorText ( $ ( el ) . attr ( "for" ) ) ;
709+ var s = editors . getEditorText ( $ ( el ) . attr ( "for" ) ) ;
792710 var tmpId = term . split ( ":" ) [ 0 ] ;
793711 $ ( el ) . val ( term . split ( ":" ) [ 1 ] . trim ( ) ) ;
794712 $ ( el ) . attr ( "tmpId" , tmpId ) ;
@@ -805,7 +723,7 @@ $(function() {
805723 . done ( function ( data ) {
806724 let type = $ ( el ) . attr ( "for" ) ;
807725 let text = data . templates [ 0 ] . text ;
808- setEditorContents ( text , type , false ) ;
726+ editors . setEditorContents ( type , text , false ) ;
809727 } ) ;
810728 }
811729
@@ -815,9 +733,9 @@ $(function() {
815733 action : function ( ) {
816734 $ . get ( 'tempSearchDetail?tmpId=' + tmpId )
817735 . done ( function ( data ) {
818- var text = "<br />" + getEditorText ( $ ( el ) . attr ( "for" ) ) ;
736+ var text = "<br />" + editors . getEditorText ( $ ( el ) . attr ( "for" ) ) ;
819737 let type = $ ( el ) . attr ( "for" ) ;
820- setEditorContents ( text , type , false ) ;
738+ editors . setEditorContents ( type , text , false ) ;
821739 } ) ;
822740 }
823741
@@ -827,9 +745,9 @@ $(function() {
827745 action : function ( ) {
828746 $ . get ( 'tempSearchDetail?tmpId=' + tmpId )
829747 . done ( function ( data ) {
830- var text = getEditorText ( $ ( el ) . attr ( "for" ) ) + "<br />" ;
748+ var text = editors . getEditorText ( $ ( el ) . attr ( "for" ) ) + "<br />" ;
831749 let type = $ ( el ) . attr ( "for" ) ;
832- setEditorContents ( text , type , false ) ;
750+ editors . setEditorContents ( type , text , false ) ;
833751 } ) ;
834752 }
835753
@@ -846,7 +764,7 @@ $(function() {
846764 . done ( function ( data ) {
847765 let type = $ ( el ) . attr ( "for" ) ;
848766 let text = data . templates [ 0 ] . text ;
849- setEditorContents ( text , type , false ) ;
767+ editors . setEditorContents ( type , text , false ) ;
850768 } ) ;
851769 }
852770
0 commit comments