Summernote个性化定制使用帮助(二)

本文详细介绍了Summernote编辑器中如何进行图片插入、回调函数的使用,包括onChange、onInit等事件,以及如何定义和在选项中使用自定义按钮,帮助用户实现更个性化的编辑体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图片插入:

insertImage

Insert an image.

// @param {String} url
// @param {String|Function} filename - optional
$('#summernote').summernote('insertImage', url, filename);

You can modify image with passing callback as second argument.

$('#summernote').summernote('insertImage', url, function ($image) {
  $image.css('width', $image.width() / 3);
  $image.attr('data-filename', 'retriever');
});

insertNode

Insert an element or textnode.

var node = document.createElement('div');
// @param {Node} node
$('#summernote').summernote('insertNode', node);

insertText

Insert text.

// @param {String} text
$('#summernote').summernote('insertText', 'Hello, world');

pasteHTML

Paste HTML string.

// @param {String} HTML string
var HTMLstring = '<div><p>Hello, world</p><p>Summernote can insert HTML string</p></div>';
$('#summernote').summernote('pasteHTML', HTMLstring);

Callbacks

Summernote support initialize callbacks and jquery’s custom event style callbacks.

Position of callbacks in options is changed after v0.7.0

After v0.7.0, every callbacks should be wrapped by callbacks object.

Callback only works with camel case string after v0.6.5

Lowercase string has been used for basic event name(ex: oninitonenteronfocusonbluronkeyuponkeydownonpaste). In contrast, callbacks name for advanced feature has been used with camel case string. This is inconsistent and confusing to use. So we rename all lowercase callback to camel case string.

onBeforeCommand

WIP: Need to work on an explanation

onChange

  • IE9-10: DOMCharacterDataModified, DOMSubtreeModified, DOMNodeInserted
  • Chrome, FF: input
// onChange callback
$('#summernote').summernote({
  callbacks: {
    onChange: function(contents, $editable) {
      console.log('onChange:', contents, $editable);
    }
  }
});

// summernote.change
$('#summernote').on('summernote.change', function(we, contents, $editable) {
  console.log('summernote\'s content is changed.');
});

onChangeCodeview

WIP: Need to work on an explanation

onDialogShown

WIP: Need to work on an explanation

onEnter

// onEnter callback
$('#summernote').summernote({
  callbacks: {
    onEnter: function() {
      console.log('Enter/Return key pressed');
    }
  }
});

// summernote.enter
$('#summernote').on('summernote.enter', function() {
  console.log('Enter/Return key pressed');
});

onFocus, onBlur, onBlurCodeview

// onFocus callback
$('#summernote').summernote({
  callbacks: {
    onFocus: function() {
      console.log('Editable area is focused');
    }
  }
});

// summernote.focus
$('#summernote').on('summernote.focus', function() {
  console.log('Editable area is focused');
});
// onBlur callback
$('#summernote').summernote({
  callbacks: {
    onBlur: function() {
      console.log('Editable area loses focus');
    }
  }
});

// summernote.blur
$('#summernote').on('summernote.blur', function() {
  console.log('Editable area loses focus');
});
// onBlurCodeview callback
$('#summernote').summernote({
  callbacks: {
    onBlurCodeview: function() {
      console.log('Codeview area loses focus');
    }
  }
});

// summernote.blur.codeview
$('#summernote').on('summernote.blur.codeview', function() {
  console.log('Codeview area loses focus');
});

onImageLinkInsert

WIP: Need to work on an explanation

onImageUpload

Override image upload handler(default: base64 dataURL on IMG tag). You can upload image to server or AWS S3: more…

// onImageUpload callback
$('#summernote').summernote({
  callbacks: {
    onImageUpload: function(files) {
      // upload image to server and create imgNode...
      $summernote.summernote('insertNode', imgNode);
    }
  }
});

// summernote.image.upload
$('#summernote').on('summernote.image.upload', function(we, files) {
  // upload image to server and create imgNode...
  $summernote.summernote('insertNode', imgNode);
});

onImageUploadError

WIP: Need to work on an explanation

onInit

// onInit callback
$('#summernote').summernote({
  callbacks: {
    onInit: function() {
      console.log('Summernote is launched');
    }
  }
});

// summernote.init
$('#summernote').on('summernote.init', function() {
  console.log('Summernote is launched');
});

onKeyup, onKeydown

// onKeyup callback
$('#summernote').summernote({
  callbacks: {
    onKeyup: function(e) {
      console.log('Key is released:', e.keyCode);
    }
  }
});

// summernote.keyup
$('#summernote').on('summernote.keyup', function(we, e) {
  console.log('Key is released:', e.keyCode);
});
// onKeydown callback
$('#summernote').summernote({
  callbacks: {
    onKeydown: function(e) {
      console.log('Key is downed:', e.keyCode);
    }
  }
});

// summernote.keydown
$('#summernote').on('summernote.keydown', function(we, e) {
  console.log('Key is downed:', e.keyCode);
});

onMouseDown,onMouseUp

onPaste

// onPaste callback
$('#summernote').summernote({
  callbacks: {
    onPaste: function(e) {
      console.log('Called event paste');
    }
  }
});

// summernote.paste
$('#summernote').on('summernote.paste', function(e) {
  console.log('Called event paste');
});

onScroll

WIP: Need to work on an explanation

Custom button

Summernote also supports custom buttons. If you want to create your own button, you can simply define and use with options.

Define button

You can create a button object with $.summernote.ui. This buttons objects have the below properties.

  • contents: contents to be displayed on the button
  • tooltip: tooltip text when mouse over
  • click: callback function to be called when mouse is clicked

Below codes is about simple button for inserting text ‘hello’.

var HelloButton = function (context) {
  var ui = $.summernote.ui;

  // create button
  var button = ui.button({
    contents: '<i class="fa fa-child"/> Hello',
    tooltip: 'hello',
    click: function () {
      // invoke insertText method with 'hello' on editor module.
      context.invoke('editor.insertText', 'hello');
    }
  });

  return button.render();   // return button as jquery object
}

You can see render() which returns jquery object as button.

Using button with options

Let’s learn how to use the button on toolbar.

First, you can define buttons with option named buttons which is a set of key-value. You can define custom button on toolbar options.

$('.summernote').summernote({
  toolbar: [
    ['mybutton', ['hello']]
  ],

  buttons: {
    hello: HelloButton
  }
});

You can also use custom button on popover in the same way.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值