mirror of
https://github.com/flynx/pWiki.git
synced 2025-12-25 20:31:58 +00:00
Compare commits
No commits in common. "44fb61bb46c799bcc2bf540fab4c5518766d4bdf" and "33961a0b3f09a0e612147cfac38d80bb8604ddc5" have entirely different histories.
44fb61bb46
...
33961a0b3f
@ -44,6 +44,11 @@ var getCharOffset = function(elem, x, y, data){
|
|||||||
?? 0
|
?? 0
|
||||||
// text node...
|
// text node...
|
||||||
if(e instanceof Text){
|
if(e instanceof Text){
|
||||||
|
// count "virtual" newlines between text and block elements...
|
||||||
|
if(data.prev_elem == 'block'){
|
||||||
|
data.c += 1 }
|
||||||
|
data.prev_elem = 'text'
|
||||||
|
|
||||||
var rect, cursor_line, line_start, offset
|
var rect, cursor_line, line_start, offset
|
||||||
for(var i=0; i < e.length; i++){
|
for(var i=0; i < e.length; i++){
|
||||||
r.setStart(e, i)
|
r.setStart(e, i)
|
||||||
@ -96,9 +101,34 @@ var getCharOffset = function(elem, x, y, data){
|
|||||||
&& ' \t\n'.includes(data.last)){
|
&& ' \t\n'.includes(data.last)){
|
||||||
return data.c - 1 }
|
return data.c - 1 }
|
||||||
|
|
||||||
|
// count "virtual" newlines between text and block elements...
|
||||||
|
var type = getComputedStyle(e).display
|
||||||
|
var block = [
|
||||||
|
'block',
|
||||||
|
// XXX these do not add up yet...
|
||||||
|
//'table',
|
||||||
|
//'table-row',
|
||||||
|
//'table-cell',
|
||||||
|
'flex',
|
||||||
|
'grid',
|
||||||
|
].includes(type)
|
||||||
|
if(block
|
||||||
|
&& data.prev_elem
|
||||||
|
&& data.prev_elem != 'block'){
|
||||||
|
data.c += 1 }
|
||||||
|
data.prev_elem = block ?
|
||||||
|
'block'
|
||||||
|
: 'elem'
|
||||||
|
|
||||||
// handle the node...
|
// handle the node...
|
||||||
data = getCharOffset(e, x, y, data)
|
data = getCharOffset(e, x, y, data)
|
||||||
|
|
||||||
|
// compensate for table stuff...
|
||||||
|
if(type == 'table-row'){
|
||||||
|
data.c -= 1 }
|
||||||
|
if(type == 'table-cell'){
|
||||||
|
data.c += 1 }
|
||||||
|
|
||||||
if(typeof(data) != 'object'){
|
if(typeof(data) != 'object'){
|
||||||
return data } } }
|
return data } } }
|
||||||
return arguments.length > 3 ?
|
return arguments.length > 3 ?
|
||||||
@ -116,7 +146,8 @@ var getCharOffset = function(elem, x, y, data){
|
|||||||
// |
|
// |
|
||||||
// markdown: '# Hea|ding'
|
// markdown: '# Hea|ding'
|
||||||
//
|
//
|
||||||
// XXX should this be replaced with offsetAt(..)???
|
// XXX we are not checking both lengths of markdown AND text...
|
||||||
|
// XXX
|
||||||
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
|
||||||
@ -132,17 +163,40 @@ var getMarkdownOffset = function(markdown, text, i){
|
|||||||
if(m >= markdown.length){
|
if(m >= markdown.length){
|
||||||
m = p } }
|
m = p } }
|
||||||
return m - t }
|
return m - t }
|
||||||
|
/*/
|
||||||
|
// XXX when one string is guaranteed to be a strict subset of the other
|
||||||
|
// this is trivial, but in the general case we can have differences
|
||||||
|
// both ways, for example the "virtual" newlines added by block
|
||||||
|
// elements mess up the text...
|
||||||
|
// ...so this in the current form is fundamentally broken as skipping
|
||||||
|
// chars in text can lead to false positives and lots of potential
|
||||||
|
// (not implemented) backtracking...
|
||||||
|
// ...needs thought...
|
||||||
|
// Q: can we cheat with this? =)
|
||||||
|
// XXX BUG: clicking right of last line places the caret before the last char...
|
||||||
|
var getMarkdownOffset = function(markdown, text, i){
|
||||||
|
i = i ?? text.length
|
||||||
|
var map = []
|
||||||
|
for(var t=0, m=0; t <= text.length; t++, m++){
|
||||||
|
var o = 0
|
||||||
|
while(text[t] != markdown[m+o]
|
||||||
|
&& m+o < markdown.length){
|
||||||
|
o++ }
|
||||||
|
if(m+o >= markdown.length){
|
||||||
|
m--
|
||||||
|
} else {
|
||||||
|
m += o }
|
||||||
|
map[t] = m - t
|
||||||
|
}
|
||||||
|
return map[i] }
|
||||||
|
//*/
|
||||||
|
|
||||||
// NOTE: this is the same as .innerText but will not add extra "\n" after
|
var getText = function(elem, res=[]){
|
||||||
// each block element...
|
|
||||||
var getTexts = function(elem, res=[]){
|
|
||||||
for(var n of elem.childNodes){
|
for(var n of elem.childNodes){
|
||||||
n.nodeType == n.TEXT_NODE ?
|
n.nodeType == n.TEXT_NODE ?
|
||||||
res.push(n.textContent)
|
res.push(n.textContent)
|
||||||
: getTexts(n, res) }
|
: getText(n, res) }
|
||||||
return res }
|
return res }
|
||||||
var getText = function(elem){
|
|
||||||
return getTexts(elem).join('') }
|
|
||||||
|
|
||||||
var offsetAt = function(A, B, i){
|
var offsetAt = function(A, B, i){
|
||||||
i ??= A.length-1
|
i ??= A.length-1
|
||||||
@ -175,7 +229,6 @@ var offsetAt = function(A, B, i){
|
|||||||
// // this should reproduce common sections...
|
// // this should reproduce common sections...
|
||||||
// console.log('---', o.map(function(e, i){ return m[i+e] }).join(''))
|
// console.log('---', o.map(function(e, i){ return m[i+e] }).join(''))
|
||||||
// XXX can we cheat here???
|
// XXX can we cheat here???
|
||||||
// XXX do we need this???
|
|
||||||
var offsetMap = function(A, B, m=[]){
|
var offsetMap = function(A, B, m=[]){
|
||||||
var o = 0
|
var o = 0
|
||||||
var p = 0
|
var p = 0
|
||||||
@ -193,6 +246,31 @@ var offsetMap = function(A, B, m=[]){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// find all common sections...
|
||||||
|
// XXX test...
|
||||||
|
function cs(A, B){
|
||||||
|
var map = []
|
||||||
|
for(var i=0; i < A.length; i++){
|
||||||
|
for(var j=0; j < B.length; j++){
|
||||||
|
var l = 0
|
||||||
|
while(A[i+j+l] == B[j+l]
|
||||||
|
&& j+l < B.length){
|
||||||
|
l++ }
|
||||||
|
if(l > 0){
|
||||||
|
map.push([i, j, l])
|
||||||
|
// Q: can we skip here? ...will skipping
|
||||||
|
// prevent matching "ababc" with "abc"??
|
||||||
|
j += l } } }
|
||||||
|
return map }
|
||||||
|
|
||||||
|
// build chains of blocks with their lengths...
|
||||||
|
function comb(map){
|
||||||
|
XXX
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Plugins...
|
// Plugins...
|
||||||
|
|
||||||
@ -2414,7 +2492,7 @@ var Outline = {
|
|||||||
var view = that.get(elem).querySelector('.view')
|
var view = that.get(elem).querySelector('.view')
|
||||||
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, getText(view), c)
|
var m = getMarkdownOffset(elem.value, view.innerText, c)
|
||||||
// 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()
|
||||||
|
|||||||
@ -48,24 +48,13 @@ var setup = function(){
|
|||||||
-
|
-
|
||||||
- ## Bugs:
|
- ## Bugs:
|
||||||
focused:: true
|
focused:: true
|
||||||
- DONE BUG: caret positioning broken (ASAP CLEANUP: GETTEXT)
|
- BUG: caret positioning broken
|
||||||
collapsed:: true
|
- new strategy: try and build offset maps on parse...
|
||||||
- Strategies to test::
|
|
||||||
- ASAP use `getText(..)` to build the input text instead of `.innerText`
|
|
||||||
- DONE adds 1 char offset per block element
|
|
||||||
- this greatly simplifies things...
|
|
||||||
- normalize `.innerText` to remove duplicate `"\n"`'s
|
|
||||||
_(will break placement on empty lines... ???)_
|
|
||||||
- try and build offset maps on parse
|
|
||||||
_(potentially too complicated)_
|
|
||||||
- *TODO*::
|
- *TODO*::
|
||||||
- *DONE*::
|
|
||||||
collapsed:: true
|
|
||||||
- ```
|
- ```
|
||||||
text text text
|
text text text
|
||||||
```
|
```
|
||||||
text text text (a click here is offset right)
|
text text text (a click here is offset right)
|
||||||
collapsed:: true
|
|
||||||
- the offset's amount depends on where in the text we click after the code block, the farther right the greater the offset...
|
- the offset's amount depends on where in the text we click after the code block, the farther right the greater the offset...
|
||||||
- `getCharOffset(..)` produces correct results, the problem is in `getMarkdownOffset(..)`
|
- `getCharOffset(..)` produces correct results, the problem is in `getMarkdownOffset(..)`
|
||||||
- text text text
|
- text text text
|
||||||
@ -73,7 +62,6 @@ var setup = function(){
|
|||||||
block element
|
block element
|
||||||
</div>
|
</div>
|
||||||
this line, and above placement of completely broken
|
this line, and above placement of completely broken
|
||||||
collapsed:: true
|
|
||||||
- the odd thing is that a nested (bug) qupted text below also breaks...
|
- the odd thing is that a nested (bug) qupted text below also breaks...
|
||||||
- _this seems to be an issue with: `.getMarkdownOffset(..)`_
|
- _this seems to be an issue with: `.getMarkdownOffset(..)`_
|
||||||
- ```
|
- ```
|
||||||
@ -88,6 +76,8 @@ var setup = function(){
|
|||||||
this returns `69` while it should return `5`
|
this returns `69` while it should return `5`
|
||||||
_...replacing `\n\n\n` with `\n\n` seems to fix the issue (also works with spaces)_
|
_...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)
|
(BUG also the above line is not italic -- can't reproduce)
|
||||||
|
- *DONE*::
|
||||||
|
collapsed:: true
|
||||||
- text text text
|
- text text text
|
||||||
- text text text
|
- text text text
|
||||||
text text text
|
text text text
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user