@@ -98,33 +98,36 @@ class JsEditorComponent(engine: EditorEngine, rootCellCreator: (EditorState) ->
98
98
}
99
99
100
100
fun processClick (event : MouseEvent ): Boolean {
101
- val target = event.target ? : return false
102
- val htmlElement = target as ? HTMLElement
103
- val producer: IProducesHtml = htmlElement?.let { GeneratedHtmlMap .getProducer(it) } ? : return false
104
- val absoluteClickX = event.clientX
105
- when (producer) {
106
- is LayoutableCell -> {
107
- val layoutable = producer as ? LayoutableCell ? : return false
108
- val text = htmlElement.innerText
109
- val cellAbsoluteBounds = htmlElement.getAbsoluteInnerBounds()
110
- val relativeClickX = absoluteClickX - cellAbsoluteBounds.x
111
- val characterWidth = cellAbsoluteBounds.width / text.length
112
- val caretPos = (relativeClickX / characterWidth).roundToInt()
113
- .coerceAtMost(layoutable.cell.getMaxCaretPos())
114
- changeSelection(CaretSelection (layoutable, caretPos))
115
- return true
101
+ val absoluteClickX = event.pageX
102
+ val targets = document.elementsFromPoint(event.clientX.toDouble(), event.clientY.toDouble())
103
+ for (target in targets) {
104
+ val htmlElement = target as ? HTMLElement
105
+ val producer: IProducesHtml = htmlElement?.let { GeneratedHtmlMap .getProducer(it) } ? : continue
106
+ when (producer) {
107
+ is LayoutableCell -> {
108
+ val layoutable = producer as ? LayoutableCell ? : continue
109
+ val text = htmlElement.innerText
110
+ val cellAbsoluteBounds = htmlElement.getAbsoluteInnerBounds()
111
+ val relativeClickX = absoluteClickX - cellAbsoluteBounds.x
112
+ val characterWidth = cellAbsoluteBounds.width / text.length
113
+ val caretPos = (relativeClickX / characterWidth).roundToInt()
114
+ .coerceAtMost(layoutable.cell.getMaxCaretPos())
115
+ changeSelection(CaretSelection (layoutable, caretPos))
116
+ return true
117
+ }
118
+ is Layoutable -> {
119
+ if (selectClosestInLine(producer.getLine() ? : continue , absoluteClickX)) return true
120
+ }
121
+ is TextLine -> {
122
+ if (selectClosestInLine(producer, absoluteClickX)) return true
123
+ }
124
+ else -> continue
116
125
}
117
- is Layoutable -> {
118
- return selectClosestInLine(producer.getLine() ? : return false , absoluteClickX)
119
- }
120
- is TextLine -> {
121
- return selectClosestInLine(producer, absoluteClickX)
122
- }
123
- else -> return false
124
126
}
127
+ return false
125
128
}
126
129
127
- private fun selectClosestInLine (line : TextLine , absoluteClickX : Int ): Boolean {
130
+ private fun selectClosestInLine (line : TextLine , absoluteClickX : Double ): Boolean {
128
131
val words = line.words.filterIsInstance<LayoutableCell >()
129
132
val closest = words.map { it to GeneratedHtmlMap .getOutput(it)!! }.minByOrNull {
130
133
min(
0 commit comments