@@ -9,6 +9,7 @@ import "choices.js/public/assets/styles/choices.css";
99
1010require ( './bootstrap' ) ;
1111require ( './search' ) ;
12+ require ( './editor' ) ;
1213
1314// Initialise cookie consent.
1415document . addEventListener ( 'DOMContentLoaded' , ( ) => {
@@ -29,113 +30,16 @@ document.addEventListener('DOMContentLoaded', () => {
2930} ) ;
3031
3132// Create a multiselect element.
32- window . choices = ( element ) => { return new Choices ( element , { maxItemCount : 3 , removeItemButton : true } ) } ;
33+ window . choices = ( element ) => {
34+ return new Choices ( element , {
35+ maxItemCount : 3 ,
36+ removeItemButton : true
37+ } )
38+ } ;
3339
3440// Syntax highlight code blocks.
3541window . highlightCode = ( element ) => {
3642 element . querySelectorAll ( "pre code" ) . forEach ( block => {
3743 hljs . highlightBlock ( block ) ;
3844 } ) ;
39- }
40-
41- // Handle the click event of the style buttons inside the editor.
42- window . handleClick = ( style , element ) => {
43- const { styles } = editorConfig ( ) ;
44- const input = element . querySelectorAll ( "textarea" ) [ 0 ] ;
45-
46- // Get the start and end positions of the current selection.
47- const selectionStart = input . selectionStart ;
48- const selectionEnd = input . selectionEnd ;
49-
50- // Find the style in the configuration.
51- const styleFormat = styles [ style ] ;
52-
53- // Get any prefix and/or suffix characters from the selected style.
54- const prefix = styleFormat . before ? styleFormat . before : "" ;
55- const suffix = styleFormat . after ? styleFormat . after : "" ;
56-
57- // Insert the prefix at the relevant position.
58- input . value = insertCharactersAtPosition (
59- input . value ,
60- prefix ,
61- selectionStart
62- ) ;
63-
64- // Insert the suffix at the relevant position.
65- input . value = insertCharactersAtPosition (
66- input . value ,
67- suffix ,
68- selectionEnd + prefix . length
69- ) ;
70-
71- // Reselect the selection and focus the input.
72- input . setSelectionRange (
73- selectionStart + prefix . length ,
74- selectionEnd + prefix . length
75- ) ;
76- input . focus ( ) ;
77- }
78-
79- // Insert provided characters at the desired place in a string.
80- const insertCharactersAtPosition = ( string , character , position ) => {
81- return [
82- string . slice ( 0 , position ) ,
83- character ,
84- string . slice ( position )
85- ] . join ( "" ) ;
86- }
87-
88- // Configuration object for the text editor.
89- window . editorConfig = ( ) => {
90- return {
91- styles : {
92- header : {
93- before : "### " ,
94- class : {
95- "fa-header" : true
96- }
97- } ,
98- bold : {
99- before : "**" ,
100- after : "**" ,
101- class : {
102- "fa-bold" : true
103- }
104- } ,
105- italic : {
106- before : "_" ,
107- after : "_" ,
108- class : {
109- "fa-italic" : true
110- }
111- } ,
112- quote : {
113- before : "> " ,
114- class : {
115- "fa-quote-left" : true
116- }
117- } ,
118- code : {
119- before : "`" ,
120- after : "`" ,
121- class : {
122- "fa-code" : true
123- }
124- } ,
125- link : {
126- before : "[](" ,
127- after : ")" ,
128- class : {
129- "fa-link" : true
130- }
131- } ,
132- image : {
133- before : "" ,
135- class : {
136- "fa-file-image-o" : true
137- }
138- }
139- }
140- }
14145}
0 commit comments