2016-12-26 19 views
3

我在我的summernote工具欄中啓用了codeview啓用codeview時無法調用editor.insertText

以及針對自定義菜單代碼(插入自定義參數編輯器):

let event = ui.buttonGroup([ 
    ui.button({ 
     contents: '<span>Custom parameters</span> <span class="note-icon-caret"></span>', 
     tooltip: 'Custom parameters', 
     className: 'btn-codeview', // <== this is just to not disable the menu when codeview is enabled 
     data: { 
      toggle: 'dropdown' 
     } 
    }), 
    ui.dropdown({ 
     items: ['one', 'two'], 
     callback: (items) => { 
      $(items).find('li a').on('click', (e) => { 
       context.invoke('editor.insertText', ` ${e.target.innerText} `); 
       e.preventDefault(); 
      }) 
     } 
    }) 
]); 

它正常工作時,代碼查看被禁用(粘貼我的自定義one並在編輯器two PARAMS),但是當我啓用codeview並點擊我的菜單項,它不會插入任何內容。

下面的代碼片段被調用,但沒有任何反應:

context.invoke('editor.insertText', ` ${e.target.innerText} `); 

啓用代碼查看時,我怎樣才能插入我的自訂參數?


UPD:的想法是有一個簡單的切換文本模式沒有HTML一個工具欄中的按鈕,並有可用的菜單(插入編輯器中自定義的話)。

+0

[這可能會幫助你引用這個鏈接的答案](http:// stackoverflow。 com/questions/26822724/summernote-angle-custom-button-in-toolbar/40830317#40830317) – Varsha

回答

0

有兩種解決方法可以做到這一點,因爲summernote不提供默認方式以編程方式「刷新」最終的textarea

1)快速度禁用/啓用代碼查看

var codeviewOn = false; // global var 

$(items).find('li a').on('click', (e) => { 
    // Check if the editor for code is activated 
    codeviewOn = $('#summernote').summernote('codeview.isActivated'); 
    // ... deactivate it for awhile 
    if (codeviewOn) 
     $('#summernote').summernote('codeview.deactivate'); 

    // insert the text 
    context.invoke('editor.insertText', e.target.innerText + " "); 

    // ... and activate it again 
    if (codeviewOn) 
     $('#summernote').summernote('codeview.activate'); 

    e.preventDefault(); 
}); 

2)直接更新最終textarea

$(items).find('li a').on('click', (e) => { 
    var codeviewOn = $('#summernote').summernote('codeview.isActivated'); 

    // always insert the text, so this will update the hidden .note-editable div 
    context.invoke('editor.insertText', e.target.innerText + " "); 

    // and update the codable textarea value directly with the editable html 
    if (codeviewOn) 
     $('.note-codable')[0].value = $('.note-editable').html(); // The [0] is getting the first texarea, so be careful if you have more than one. 

    e.preventDefault(); 
}) 

退房此最後一個選項的的jsfiddle:https://jsfiddle.net/3b93w1L3/

+0

謝謝!但這兩種方法都添加了P標籤,丟失了光標並且沒有在原始光標位置之後插入( –