Skip to content

Commit

Permalink
ts
Browse files Browse the repository at this point in the history
  • Loading branch information
caiwuu committed Sep 11, 2023
1 parent 586a34d commit d667fba
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 147 deletions.
14 changes: 7 additions & 7 deletions src/core/defaultActions/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
import { del } from './delete'
import { isPrimitive, times } from '../utils'


/**
* @description 文本输入
* @param {*} range
* @param {*} data
*/
function inputText (range, data) {
function inputText(range, data, ts) {
let { startContainer: path } = range
if (path) {
if (path.vn.type !== 'text') {
path = path.firstLeaf
range.setEnd(path, 0)
}
const component = path.parent.component
component.contentInput(path, range, data)
component.contentInput(path, range, data, ts)
} else {
console.error('无效path')
}
Expand All @@ -31,9 +30,9 @@ function inputText (range, data) {
/**
* @description 操作转换
* @param {*} e
* @returns {*}
* @returns {*}
*/
function transformOps (e) {
function transformOps(e) {
if (isPrimitive(e)) {
return {
type: 'input',
Expand All @@ -49,7 +48,8 @@ function transformOps (e) {
* @param {*} range
* @param {*} e
*/
export function input (range, e) {
export function input(range, e, ts) {
debugger
const { data, type } = transformOps(e)
if (!range.collapsed) del(range, true)
if (type === 'input') {
Expand All @@ -66,7 +66,7 @@ export function input (range, e) {
}
console.log(prevInputValue.length)
times(prevInputValue.length, del, range.editor, range, true)
inputText(range, inputData)
inputText(range, inputData, ts)
} else if (type === 'compositionstart') {
// console.log('开始聚合输入:', data)
range.inputState.isComposing = true
Expand Down
51 changes: 31 additions & 20 deletions src/core/initCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Selection from './selection'
import { usePlugin } from './pluginContext'
import Formater from '@/core/model/formater'
import History from './history/index'
import Transaction from './transform/transaction'

/**
* @description 内核初始化
Expand Down Expand Up @@ -45,26 +46,36 @@ function initDispatcher(editor) {
}
})
editor.on('keyboardEvents', (event) => {
editor.selection.ranges.forEach((range) => {
const path = range.container
// 支持简写handle
const quickEventHandle = event.key
? path.component[`on${titleCase(event.type)}${event.key}`]?.bind(path.component)
: null
let eventHandle
// 处理聚合输入
if (event.type.startsWith('composition')) {
eventHandle = path.component.onInput?.bind(path.component)
} else {
eventHandle = path.component[`on${titleCase(event.type)}`]?.bind(path.component)
}
if (typeof eventHandle === 'function') {
eventHandle(range, event)
}
if (event.key && typeof quickEventHandle === 'function') {
quickEventHandle(range, event)
}
})
// 输入处理
if (event.type.startsWith('composition') || event.type === 'input') {
debugger
const ts = new Transaction(editor)
editor.selection.ranges.forEach((range) => {
const path = range.container
const eventHandle = path.component.onInput?.bind(path.component)
if (typeof eventHandle === 'function') {
console.log(ts)
eventHandle(range, event, ts)
}
})
} else {
// 其他键盘事件处理
editor.selection.ranges.forEach((range) => {
const path = range.container
// 支持简写handle
console.log(event.type)
const quickEventHandle = event.key
? path.component[`on${titleCase(event.type)}${event.key}`]?.bind(path.component)
: null
const eventHandle = path.component[`on${titleCase(event.type)}`]?.bind(path.component)
if (typeof eventHandle === 'function') {
eventHandle(range, event)
}
if (typeof quickEventHandle === 'function') {
quickEventHandle(range, event)
}
})
}
})
editor.on('selectionchange-origin', () => {
const nativeSelection = pluginContext.platform.nativeSelection
Expand Down
Loading

0 comments on commit d667fba

Please sign in to comment.