Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tabSize setting #342

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ $ npm install mavon-editor --save
| ishljs | Boolean | true | highlight code switch |
| imageFilter | Function | null | Image file filter Function, params is a `File Object`, you should return `Boolean` about the test result |
| imageClick | function | null | Image Click Function |
| tabSize | Number | null | How many spaces equals one tab, default \t |
| toolbars | Object | As in the following example | toolbars |

```javascript
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ $ npm install mavon-editor --save
| ishljs | Boolean | true | 代码高亮 |
| imageFilter | function | null | 图片过滤函数,参数为一个`File Object`,要求返回一个`Boolean`, `true`表示文件合法,`false`表示文件不合法 |
| imageClick | function | null | 图片点击事件,默认为预览,可覆盖 |
| tabSize | Number | \t | tab转化为几个空格,默认为\t |
| toolbars | Object | 如下例 | 工具栏 |

```javascript
Expand Down
21 changes: 12 additions & 9 deletions src/lib/core/extra-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export const insertUl = ($vm) => {
obj.focus();
}
// 插入tab
export const insertTab = ($vm) => {
export const insertTab = ($vm, tab) => {
tab = tab ? (new Array(tab)).fill(' ').join('') : '\t'
let obj = $vm.getTextareaDom();
if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
var startPos = obj.selectionStart;
Expand All @@ -164,14 +165,14 @@ export const insertTab = ($vm) => {
if (lastLine.match(/^\s*[0-9]+\.\s+\S*/)) {
// 有序列表
let temp = lastLine.replace(/(\d+)/, 1)
obj.value = tmpStr.substring(0, startPos - temp.length) + '\t' + temp + tmpStr.substring(endPos, tmpStr.length);
obj.value = tmpStr.substring(0, startPos - temp.length) + tab + temp + tmpStr.substring(endPos, tmpStr.length);
} else if (lastLine.match(/^\s*-\s+\S*/)) {
// 无序列表
obj.value = tmpStr.substring(0, startPos - lastLine.length) + '\t' + lastLine + tmpStr.substring(endPos, tmpStr.length);
obj.value = tmpStr.substring(0, startPos - lastLine.length) + tab + lastLine + tmpStr.substring(endPos, tmpStr.length);
} else {
obj.value = tmpStr.substring(0, startPos) + '\t' + tmpStr.substring(endPos, tmpStr.length);
obj.value = tmpStr.substring(0, startPos) + tab + tmpStr.substring(endPos, tmpStr.length);
}
obj.selectionStart = obj.selectionEnd = startPos + 1;
obj.selectionStart = obj.selectionEnd = startPos + tab.length;
} else {
alert('Error: Browser version is too low')
// obj.value += str;
Expand All @@ -181,17 +182,19 @@ export const insertTab = ($vm) => {
obj.focus();
}
// shift + tab
export const unInsertTab = ($vm) => {
export const unInsertTab = ($vm, tab) => {
let regTab = new RegExp(tab ? `\\s{${tab}}` : '\t')
console.log(`regTab:`, regTab)
let obj = $vm.getTextareaDom();
if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
var startPos = obj.selectionStart;
var endPos = obj.selectionEnd;
var tmpStr = obj.value;
let lastLine = tmpStr.substring(0, startPos).split('\n').pop()
if (lastLine.search(/\t/) >= 0) {
if (lastLine.search(regTab) >= 0) {
// 替换最后一个制表符为空
obj.value = tmpStr.substring(0, startPos - lastLine.length) + lastLine.replace(/(.*)\t/, '$1') + tmpStr.substring(endPos, tmpStr.length);
obj.selectionStart = obj.selectionEnd = startPos - 1;
obj.value = tmpStr.substring(0, startPos - lastLine.length) + lastLine.replace(regTab, '') + tmpStr.substring(endPos, tmpStr.length);
obj.selectionStart = obj.selectionEnd = startPos - (tab || 1);
}
} else {
alert('Error: Browser version is too low')
Expand Down
10 changes: 7 additions & 3 deletions src/mavon-editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export default {
imageClick: {
type: Function,
default: null
},
tabSize: {
type: Number,
default: 0
}
},
data() {
Expand Down Expand Up @@ -296,7 +300,7 @@ export default {
// 图片预览事件监听
ImagePreviewListener(this);
// 设置默认焦点
if(this.autofocus) {
if (this.autofocus) {
this.getTextareaDom().focus();
}
// fullscreen事件
Expand Down Expand Up @@ -537,7 +541,7 @@ export default {
insertTextAtCaret(obj, {prefix, subfix, str, type}, this);
},
insertTab() {
insertTab(this)
insertTab(this, this.tabSize)
},
insertOl() {
insertOl(this)
Expand All @@ -549,7 +553,7 @@ export default {
insertUl(this)
},
unInsertTab() {
unInsertTab(this)
unInsertTab(this, this.tabSize)
},
insertEnter(event) {
insertEnter(this, event)
Expand Down