Commit ac7ca248 by jml0128

fix

1 parent cedf1652
{
"name": "xrk-bi",
"version": "0.1.3",
"version": "0.1.4",
"description": "xrk-bi",
"author": "xrk",
"main": "dist/bundler.js",
......
......@@ -205,9 +205,9 @@ export const keepLastIndex = (obj, focusOffset) => {
export const initDivInput = (
dom,
options = { useWrap: false, usePasteStyle: false, callback: () => {} }
options = { useWrap: false, usePasteStyle: false }
) => {
const { useWrap, usePasteStyle, callback } = options;
const { useWrap, usePasteStyle } = options;
const maxlength = dom.getAttribute('maxlength');
let baseIndex = 0;
let focusIndex = 0;
......@@ -216,10 +216,11 @@ export const initDivInput = (
let deleteText = '';
let isComposition = false;
dom.innerHTML = dom.innerText;
const setIndex = function() {
const { innerText } = this;
const { baseOffset, focusOffset } = getSelection();
[baseIndex, focusIndex] = [baseOffset, focusOffset].sort((a, b) => a - b);
const { anchorOffset, focusOffset } = getSelection();
[baseIndex, focusIndex] = [anchorOffset, focusOffset].sort((a, b) => a - b);
leftText = innerText.slice(0, baseIndex);
rightText = innerText.slice(focusIndex);
deleteText = innerText.slice(baseIndex, focusIndex);
......@@ -255,24 +256,26 @@ export const initDivInput = (
isComposition = false;
dealText.call(this, event.data);
});
dom.addEventListener('beforeinput', function(event) {
dom.addEventListener('keydown', function(event) {
if (isComposition) {
return;
}
const { inputType } = event;
if (inputType != 'insertCompositionText') {
const { key, ctrlKey } = event;
if (key != 'Process') {
setIndex.call(this);
}
if (
this.innerText.length - (focusIndex - baseIndex) >= maxlength &&
['insertText', 'insertCompositionText', 'insertParagraph'].includes(
inputType
)
!['Control', 'Backspace', 'Process', 'Enter'].includes(key) &&
!(ctrlKey && ['a', 'A'].includes(key)) && // 全选
!(ctrlKey && ['x', 'X'].includes(key)) && // 剪切
!(ctrlKey && ['c', 'C'].includes(key)) && // 复制
!(ctrlKey && ['v', 'V'].includes(key)) // 粘贴
) {
// 超出长度并且是输入操作,则禁止输入
event.preventDefault();
}
if (inputType === 'insertParagraph' && !useWrap) {
if (key === 'Enter' && !useWrap) {
// 禁止换行
event.preventDefault();
}
......@@ -298,10 +301,6 @@ export const initDivInput = (
document.execCommand('paste', false, text);
}
});
dom.addEventListener('blur', function() {
callback(this.innerText);
});
} else {
console.warn('启用usePasteStyle,在粘贴时候无法限制字符长度');
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!