@@ -97,6 +97,15 @@ window.mathquill4quill = function(dependencies) {
9797 }
9898 }
9999
100+ function removeItemFromHistoryList ( array , index ) {
101+ array . splice ( index , 1 ) ;
102+ try {
103+ localStorage . setItem ( historyCacheKey , JSON . stringify ( array ) ) ;
104+ } catch ( e ) {
105+ // eslint-disable-line no-empty
106+ }
107+ }
108+
100109 function getTooltip ( ) {
101110 return quill . container . getElementsByClassName ( "ql-tooltip" ) [ 0 ] ;
102111 }
@@ -277,8 +286,25 @@ window.mathquill4quill = function(dependencies) {
277286 button . setAttribute ( "class" , "mathquill4quill-history-button" ) ;
278287 }
279288
280- function applyHistoryContainerStyles ( container ) {
281- container . setAttribute ( "class" , "mathquill4quill-history-container" ) ;
289+ function applyHistoryInnerContainerStyles ( container ) {
290+ container . setAttribute (
291+ "class" ,
292+ "mathquill4quill-history-inner-container"
293+ ) ;
294+ }
295+
296+ function applyHistoryDeleteButtonStyles ( button ) {
297+ button . setAttribute ( "class" , "mathquill4quill-history-delete-button" ) ;
298+ button . setAttribute ( "title" , "Delete" ) ;
299+ button . setAttribute ( "role" , "button" ) ;
300+ }
301+
302+ function applyHistoryContainerStyles ( container , withDeleteButton ) {
303+ let className = "mathquill4quill-history-container" ;
304+ if ( withDeleteButton ) {
305+ className += " mathquill4quill-history-container-with-delete-button" ;
306+ }
307+ container . setAttribute ( "class" , className ) ;
282308 }
283309
284310 function createHistoryButton ( latex , mqField ) {
@@ -308,15 +334,30 @@ window.mathquill4quill = function(dependencies) {
308334
309335 historyDiv = document . createElement ( "div" ) ;
310336 let container = document . createElement ( "div" ) ;
311- applyHistoryContainerStyles ( container ) ;
337+ applyHistoryContainerStyles ( container , displayDeleteButtonOnHistory ) ;
312338 let header = document . createElement ( "p" ) ;
313339 header . innerHTML = "Past formulas (max " + historySize + ")" ;
314340 historyDiv . appendChild ( header ) ;
315341
316342 history . forEach ( element => {
317343 const button = createHistoryButton ( element , mqField ) ;
318344 applyHistoryButtonStyles ( button ) ;
319- container . appendChild ( button ) ;
345+ if ( displayDeleteButtonOnHistory ) {
346+ const innerContainer = document . createElement ( "div" ) ;
347+ applyHistoryInnerContainerStyles ( innerContainer ) ;
348+ const deleteButton = document . createElement ( "div" ) ;
349+ applyHistoryDeleteButtonStyles ( deleteButton ) ;
350+ deleteButton . addEventListener ( "click" , ( ) => {
351+ innerContainer . remove ( ) ;
352+ const index = history . indexOf ( element ) ;
353+ removeItemFromHistoryList ( history , index ) ;
354+ } ) ;
355+ innerContainer . appendChild ( button ) ;
356+ innerContainer . appendChild ( deleteButton ) ;
357+ container . appendChild ( innerContainer ) ;
358+ } else {
359+ container . appendChild ( button ) ;
360+ }
320361 } ) ;
321362 historyDiv . appendChild ( container ) ;
322363 tooltip . appendChild ( historyDiv ) ;
@@ -357,6 +398,8 @@ window.mathquill4quill = function(dependencies) {
357398 let historyList = fetchHistoryList ( historyCacheKey ) ;
358399 const historySize = options . historySize || 10 ;
359400 const displayHistory = options . displayHistory || false ;
401+ const displayDeleteButtonOnHistory =
402+ options . displayDeleteButtonOnHistory || false ;
360403
361404 const mqInput = newMathquillInput ( ) ;
362405 const operatorButtons = newOperatorButtons ( ) ;
0 commit comments