Compare commits

..

No commits in common. "54b7ae94ca1ceb3b49aebeed7d96b5bed0ff5c3b" and "10da7885acb3bd80660e5e4f66ac4011e426ab60" have entirely different histories.

2 changed files with 25 additions and 98 deletions

View File

@ -29,84 +29,52 @@ function clickPoint(x,y){
// box corresponds the to desired coordinates. This accounts for nested // box corresponds the to desired coordinates. This accounts for nested
// elements. // elements.
// //
// XXX do a binary search?? // XXX it would be a better idea to do a binary search instead of a liner
var getCharOffset = function(elem, x, y, data){ // pass...
data = data ?? {} // ...though b-search will get us to the target, we stll need to count...
var getCharOffset = function(elem, x, y, options={}){
var r = document.createRange() var r = document.createRange()
var elem_rect = data.elem_rect =
data.elem_rect
?? elem.getBoundingClientRect()
for(var e of [...elem.childNodes]){ for(var e of [...elem.childNodes]){
var c = options.c ?? 0
// text node... // text node...
if(e instanceof Text){ if(e instanceof Text){
var c = data.c = var prev, rect, cursor_line, col
data.c for(var i=0; i <= e.length; i++){
?? 0
var prev, rect, cursor_line, line_start, offset
for(var i=0; i < e.length; i++){
r.setStart(e, i) r.setStart(e, i)
r.setEnd(e, i) r.setEnd(e, i)
prev = rect prev = rect
?? data.prev ?? options.prev
rect = r.getBoundingClientRect() rect = r.getBoundingClientRect()
// line change... // line change...
// NOTE: this is almost identical to .getTextOffsetAt(..) see // NOTE: this is almost identical to .getTextOffsetAt(..) see
// that for more docs... // that for more docs...
line_start = prev if(prev
&& prev.y != rect.y && prev.y != rect.y){
if(line_start){
if(cursor_line){ if(cursor_line){
return offset return col
?? c + i - 2 } ?? c + i - 2 }
offset = undefined } col = undefined }
cursor_line = cursor_line =
rect.y <= y rect.y <= y
&& rect.bottom >= y && rect.bottom >= y
if(offset == null if(col == null
&& rect.x >= x){ && rect.x >= x){
// get closest edge of element under cursor... col = (c + i == 0
var dp = Math.abs( || Math.abs(rect.x - x) <= Math.abs(prev.x - x)) ?
(line_start ?
elem_rect
: prev).x
- x)
var dx = Math.abs(rect.x - x)
offset = dx <= dp ?
c + i c + i
: c + i - 1 : c + i - 1
if(cursor_line){ if(cursor_line){
return offset } } } return col } } }
data.c += i options.c += c - 1
data.last = e.data[i-1]
// html node... // html node...
} else { } else {
prev = data.prev = options.prev = prev
prev options = getCharOffset(e, x, y, options)
?? data.prev if(typeof(options) != 'object'){
// special case: line break between cursor line and next element... return options } } }
if(prev
// cursor line...
&& prev.y <= y
&& prev.bottom >= y
// line break...
&& prev.y < e.getBoundingClientRect().y
// no whitespace at end, no compensation needed... (XXX test)
&& ' \t\n'.includes(data.last)){
return data.c - 1 }
// block element -- compensate for a lacking '\n'...
if(['block', 'table', 'flex', 'grid']
.includes(
getComputedStyle(e).display)){
data.c += 1 }
// handle the node...
data = getCharOffset(e, x, y, data)
if(typeof(data) != 'object'){
return data } } }
// no result was found... // no result was found...
console.log('---', data)
return data.c ?? data
return arguments.length > 3 ? return arguments.length > 3 ?
data options
: null } : null }
@ -119,13 +87,12 @@ var getCharOffset = function(elem, x, y, data){
// | // |
// markdown: '# Hea|ding' // markdown: '# Hea|ding'
// //
// XXX we are not checking both lengths of markdown AND text...
var getMarkdownOffset = function(markdown, text, i){ var getMarkdownOffset = function(markdown, text, i){
i = i ?? text.length i = i ?? text.length
var m = 0 var m = 0
// walk both strings skipping/counting non-matching stuff... // walk both strings skipping/counting non-matching stuff...
for(var t=0; t <= i; t++, m++){ for(var n=0; n <= i; n++, m++){
var c = text[t] var c = text[n]
var p = m var p = m
// walk to next match... // walk to next match...
while(c != markdown[m] && m < markdown.length){ while(c != markdown[m] && m < markdown.length){
@ -134,7 +101,7 @@ var getMarkdownOffset = function(markdown, text, i){
// entity, symbol, ...) // entity, symbol, ...)
if(m >= markdown.length){ if(m >= markdown.length){
m = p } } m = p } }
return m - t } return m - n }
@ -2074,7 +2041,6 @@ var Outline = {
var initial = elem.selectionStart var initial = elem.selectionStart
var c = getCharOffset(view, evt.clientX, evt.clientY) var c = getCharOffset(view, evt.clientX, evt.clientY)
var m = getMarkdownOffset(elem.value, view.innerText, c) var m = getMarkdownOffset(elem.value, view.innerText, c)
console.log('---', c, m)
// selecting an element with text offset by markup... // selecting an element with text offset by markup...
if(m != 0){ if(m != 0){
evt.preventDefault() evt.preventDefault()

View File

@ -48,45 +48,6 @@ var setup = function(){
- -
- ## Bugs: - ## Bugs:
focused:: true focused:: true
- BUG: caret positioning broken
- Example:
- text text text
<div>
block element
</div>
this line, and above placement of completely broken
- _this seems to be an issue with: `.getMarkdownOffset(..)`_
- ```
m = `text text text
<div>
block element
</div>
this line, and above placement of completely broken`
t = 'text text text\n\n\nblock element\n\n\nthis line, and above placement of completely broken '
getMarkdownOffset(m, t, 26)
```
this returns `69` while it should return `5`
_...replacing `\n\n\n` with `\n\n` seems to fix the issue (also works with spaces)_
(BUG also the above line is not italic -- can't reproduce)
- DONE clicking right of this line will select last line of block
```
text text text
```
this line, placement is offset by 2
- DONE text text text
_text text text_
text text text
- DONE M
M can't place cursor before first char
M
- BUG: parser: code blocks do not ignore single back-quotes...
- ```
x = `moo`
```
- _this also leads to double quoting of html..._
```
x = `<i>moo</i>`
```
- BUG: mobile browsers behave quite chaotically ignoring parts of the styling... - BUG: mobile browsers behave quite chaotically ignoring parts of the styling...
- FF: - FF:
- zooming on edited field - zooming on edited field