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

Improve performance of column resizing mousemouse handler by debouncing #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions src/columnresizing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Plugin, PluginKey} from "prosemirror-state"
import {Decoration, DecorationSet} from "prosemirror-view"
import {cellAround, pointsAtCell, setAttr} from "./util"
import {cellAround, debounce, pointsAtCell, setAttr} from "./util"
import {TableMap} from "./tablemap"
import {TableView, updateColumns} from "./tableview"
import {tableNodeTypes} from "./schema"
Expand All @@ -27,7 +27,7 @@ export function columnResizing({ handleWidth = 5, cellMinWidth = 25, View = Tabl
},

handleDOMEvents: {
mousemove(view, event) { handleMouseMove(view, event, handleWidth, cellMinWidth, lastColumnResizable) },
mousemove(view, event) { debouncedHandleMouseMove(view, event, handleWidth, cellMinWidth, lastColumnResizable) },
mouseleave(view) { handleMouseLeave(view) },
mousedown(view, event) { handleMouseDown(view, event, cellMinWidth) }
},
Expand Down Expand Up @@ -93,6 +93,8 @@ function handleMouseMove(view, event, handleWidth, cellMinWidth, lastColumnResiz
}
}

const debouncedHandleMouseMove = debounce(handleMouseMove)

function handleMouseLeave(view) {
let pluginState = key.getState(view.state)
if (pluginState.activeHandle > -1 && !pluginState.dragging) updateHandle(view, -1)
Expand Down
23 changes: 23 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,26 @@ export function columnIsHeader(map, table, col) {
return false
return true
}

const defaultDebounceTime = 100
export function debounce(func, wait = defaultDebounceTime, immediate) {
let timeout
return function() {
let context = this,
args = arguments
let later = function() {
timeout = null
if (!immediate) {
func.apply(context, args)
}
}
let callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) {
func.apply(context, args)
}

return timeout
}
}