mirror of
https://github.com/marktext/marktext.git
synced 2025-05-03 20:01:34 +08:00
opti: throttle util
This commit is contained in:
parent
bdf88882c1
commit
e253ff5313
@ -47,16 +47,4 @@ export const CLASS_OR_ID = genUpper2LowerKeyHash([
|
|||||||
'AG_EMOJI_MARKED_TEXT'
|
'AG_EMOJI_MARKED_TEXT'
|
||||||
])
|
])
|
||||||
|
|
||||||
export const paragraphClassName = 'aganippe-paragraph'
|
|
||||||
export const activeClassName = 'aganippe-active'
|
|
||||||
export const EDITOR_ATTR_NAME = 'aganippe-editor-element'
|
|
||||||
export const EDITOR_ID = 'write'
|
|
||||||
export const FLOAT_BOX_ID = 'ag-float-emoji-box'
|
|
||||||
export const FLOAT_BOX_CLASSNAME = 'ag-float-emoji-box'
|
|
||||||
export const SHOW_EMOJI_BOX = 'ag-float-emoji-box-show'
|
|
||||||
export const EMOJI_ITEM = 'ag-emoji-item'
|
|
||||||
export const EMOJI_ITEM_ACTIVE = 'ag-emoji-active'
|
|
||||||
export const EMOJI_ICON = 'ag-emoji-item-icon'
|
|
||||||
export const EMOJI_MARKED_TEXT = 'ag-emoji-marked-text'
|
|
||||||
|
|
||||||
// export const markedSymbol = ['*', '-', '_', '!', '[', ']']
|
// export const markedSymbol = ['*', '-', '_', '!', '[', ']']
|
||||||
|
@ -46,7 +46,7 @@ class Aganippe {
|
|||||||
eventCenter.subscribe('markedTextChange', this.subscribeMarkedText.bind(this))
|
eventCenter.subscribe('markedTextChange', this.subscribeMarkedText.bind(this))
|
||||||
this.dispatchMarkedText()
|
this.dispatchMarkedText()
|
||||||
|
|
||||||
eventCenter.subscribe('editEmoji', throttle(this.subscribeEditEmoji.bind(this)))
|
eventCenter.subscribe('editEmoji', throttle(this.subscribeEditEmoji.bind(this), 200))
|
||||||
this.dispatchEditeEmoji()
|
this.dispatchEditeEmoji()
|
||||||
|
|
||||||
eventCenter.subscribe('enter', this.subscribeEnter.bind(this))
|
eventCenter.subscribe('enter', this.subscribeEnter.bind(this))
|
||||||
|
0
src/editor/records.js
Normal file
0
src/editor/records.js
Normal file
@ -19,13 +19,42 @@ export const getUniqueId = set => {
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
export const throttle = fn => {
|
// https://github.com/jashkenas/underscore
|
||||||
let timer = null
|
export const throttle = (func, wait = 50) => {
|
||||||
return (...args) => {
|
let context
|
||||||
if (timer) clearTimeout(timer)
|
let args
|
||||||
timer = setTimeout(() => {
|
let result
|
||||||
fn(...args)
|
let timeout = null
|
||||||
}, 300)
|
let previous = 0
|
||||||
|
const later = () => {
|
||||||
|
previous = Date.now()
|
||||||
|
timeout = null
|
||||||
|
result = func.apply(context, args)
|
||||||
|
if (!timeout) {
|
||||||
|
context = args = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return function () {
|
||||||
|
const now = Date.now()
|
||||||
|
const remaining = wait - (now - previous)
|
||||||
|
|
||||||
|
context = this
|
||||||
|
args = arguments
|
||||||
|
if (remaining <= 0 || remaining > wait) {
|
||||||
|
if (timeout) {
|
||||||
|
clearTimeout(timeout)
|
||||||
|
timeout = null
|
||||||
|
}
|
||||||
|
previous = now
|
||||||
|
result = func.apply(context, args)
|
||||||
|
if (!timeout) {
|
||||||
|
context = args = null
|
||||||
|
}
|
||||||
|
} else if (!timeout) {
|
||||||
|
timeout = setTimeout(later, remaining)
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user