Skip to content

Commit

Permalink
Merge bb14056 into 60e5b39
Browse files Browse the repository at this point in the history
  • Loading branch information
easylogic committed Nov 28, 2018
2 parents 60e5b39 + bb14056 commit 761eb15
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 33 deletions.
14 changes: 7 additions & 7 deletions lang/summernote-zh-CN.js
Expand Up @@ -27,13 +27,13 @@
shapeThumbnail: '形状: 缩略图',
shapeNone: '形状: 无',
dragImageHere: '将图片拖拽至此处',
dropImage: '拖拽图片或文本',
dropImage: '拖放图片或文字',
selectFromFiles: '从本地上传',
maximumFileSize: '文件大小最大值',
maximumFileSizeError: '文件大小超出最大值。',
url: '图片地址',
remove: '移除图片',
original: '原始图片'
original: '原始'
},
video: {
video: '视频',
Expand All @@ -53,10 +53,10 @@
},
table: {
table: '表格',
addRowAbove: '在上方插入行',
addRowBelow: '在下方插入行',
addColLeft: '在左侧插入列',
addColRight: '在右侧插入列',
addRowAbove: '上方插入行',
addRowBelow: '下方插入行',
addColLeft: '左边插入列',
addColRight: '右边插入列',
delRow: '删除行',
delCol: '删除列',
delTable: '删除表格'
Expand Down Expand Up @@ -148,7 +148,7 @@
},
specialChar: {
specialChar: '特殊字符',
select: '选取特殊字符'
select: '选择特殊字符'
}
}
});
Expand Down
5 changes: 5 additions & 0 deletions src/js/base/core/key.js
Expand Up @@ -2,6 +2,11 @@ import lists from './lists';
import func from './func';

const KEY_MAP = {
// punctuation characters
'PERIOD': 190,
'COMMA': 188,
'SEMICOLON': 186,

'BACKSPACE': 8,
'TAB': 9,
'ENTER': 13,
Expand Down
2 changes: 1 addition & 1 deletion src/js/base/module/AutoLink.js
Expand Up @@ -51,7 +51,7 @@ export default class AutoLink {

handleKeydown(e) {
if (lists.contains([key.code.ENTER, key.code.SPACE], e.keyCode)) {
const wordRange = this.context.invoke('editor.createRange').getWordRange();
const wordRange = this.context.invoke('editor.getLastRange').getWordRange();
this.lastWordRange = wordRange;
}
}
Expand Down
88 changes: 88 additions & 0 deletions src/js/base/module/AutoReplace.js
@@ -0,0 +1,88 @@
import lists from '../core/lists';
import dom from '../core/dom';
import key from '../core/key';

export default class AutoReplace {
constructor(context) {
this.context = context;
this.options = context.options.replace || {};

this.keys = [key.code.ENTER, key.code.SPACE, key.code.PERIOD, key.code.COMMA, key.code.SEMICOLON, key.code.SLASH];
this.previousKeydownCode = null;

this.events = {
'summernote.keyup': (we, e) => {
if (!e.isDefaultPrevented()) {
this.handleKeyup(e);
}
},
'summernote.keydown': (we, e) => {
this.handleKeydown(e);
}
};
}

shouldInitialize() {
return !!this.options.match;
}

initialize() {
this.lastWord = null;
}

destroy() {
this.lastWord = null;
}

/**
* options.replace.match = function(word, done){ if(word=='this') done('that') else done();
*/

replace() {
if (!this.lastWord) {
return;
}

const self = this;
const keyword = this.lastWord.toString();
this.options.match(keyword, function(match) {
if (match) {
let node = '';

if (typeof match === 'string') {
node = dom.createText(match);
} else if (match instanceof jQuery) {
node = match[0];
} else if (match instanceof Node) {
node = match;
}

if (!node) return;
self.lastWord.insertNode(node);
self.lastWord = null;
self.context.invoke('editor.focus');
}
});
}

handleKeydown(e) {
// this forces it to remember the last whole word, even if multiple termination keys are pressed
// before the previous key is let go.
if (this.previousKeydownCode && lists.contains(this.keys, this.previousKeydownCode)) {
this.previousKeydownCode = e.keyCode;
return;
}

if (lists.contains(this.keys, e.keyCode)) {
const wordRange = this.context.invoke('editor.createRange').getWordRange();
this.lastWord = wordRange;
}
this.previousKeydownCode = e.keyCode;
}

handleKeyup(e) {
if (lists.contains(this.keys, e.keyCode)) {
this.replace();
}
}
}

0 comments on commit 761eb15

Please sign in to comment.