minor tweaks + added stub toc generator...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2023-11-09 02:35:04 +03:00
parent f1288fcc4f
commit 5b0967f032
2 changed files with 67 additions and 18 deletions

View File

@ -173,6 +173,7 @@ var getMarkdownOffset = function(markdown, text, i){
// (not implemented) backtracking... // (not implemented) backtracking...
// ...needs thought... // ...needs thought...
// Q: can we cheat with this? =) // 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){ var getMarkdownOffset = function(markdown, text, i){
i = i ?? text.length i = i ?? text.length
var map = [] var map = []
@ -255,12 +256,12 @@ var blocks = {
return text return text
// markdown... // markdown...
// style: headings... // style: headings...
.replace(/^(?<!\\)######\s+(.*)$/m, this.style(editor, elem, 'heading-6')) .replace(/^(?<!\\)######\s+(.*)$/m, this.style(editor, elem, ['heading', 'heading-6']))
.replace(/^(?<!\\)#####\s+(.*)$/m, this.style(editor, elem, 'heading-5')) .replace(/^(?<!\\)#####\s+(.*)$/m, this.style(editor, elem, ['heading', 'heading-5']))
.replace(/^(?<!\\)####\s+(.*)$/m, this.style(editor, elem, 'heading-4')) .replace(/^(?<!\\)####\s+(.*)$/m, this.style(editor, elem, ['heading', 'heading-4']))
.replace(/^(?<!\\)###\s+(.*)$/m, this.style(editor, elem, 'heading-3')) .replace(/^(?<!\\)###\s+(.*)$/m, this.style(editor, elem, ['heading', 'heading-3']))
.replace(/^(?<!\\)##\s+(.*)$/m, this.style(editor, elem, 'heading-2')) .replace(/^(?<!\\)##\s+(.*)$/m, this.style(editor, elem, ['heading', 'heading-2']))
.replace(/^(?<!\\)#\s+(.*)$/m, this.style(editor, elem, 'heading-1')) .replace(/^(?<!\\)#\s+(.*)$/m, this.style(editor, elem, ['heading', 'heading-1']))
// style: list... // style: list...
//.replace(/^(?<!\\)[-\*]\s+(.*)$/m, style('list-item')) //.replace(/^(?<!\\)[-\*]\s+(.*)$/m, style('list-item'))
.replace(/^\s*(.*)(?<!\\):\s*$/m, this.style(editor, elem, 'list')) .replace(/^\s*(.*)(?<!\\):\s*$/m, this.style(editor, elem, 'list'))
@ -611,6 +612,47 @@ var tasks = {
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// XXX needs lots more work...
var toc = {
__proto__: plugin,
update: function(editor, elem){
var outline = editor.outline
var tocs = [...outline.querySelectorAll('.toc .view')]
if(tocs.length == 0){
return }
var level = function(node){
var depth = 0
var parent = node
while(parent !== outline
&& parent != null){
if(parent.classList.contains('block')){
depth++ }
parent = parent.parentElement }
return depth }
var headings = [...editor.outline.querySelectorAll('.heading .view')]
.map(function(e){
return `<li>${ e.innerText.split(/\n/)[0] }</li>`
})
var lst = document.createElement('ul')
lst.innerHTML = headings.join('\n')
for(var toc of tocs){
toc.append(lst) } },
__setup__: function(editor){
return this.update(editor) },
__editedcode__: function(evt, editor, elem){
return this.update(editor, elem) },
__parse__: function(text, editor, elem){
return text
.replace(/^\s*TOC\s*$/,
this.style(editor, elem, 'toc', '')) },
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// XXX Hackish... // XXX Hackish...
@ -1144,6 +1186,7 @@ var Outline = {
// NOTE: this needs to be before styling to prevent it from // NOTE: this needs to be before styling to prevent it from
// treating '[_] ... [_]' as italic... // treating '[_] ... [_]' as italic...
tasks, tasks,
//toc,
styling, styling,
// XXX // XXX
//attributes, //attributes,
@ -2088,7 +2131,8 @@ var Outline = {
var a = edited.selectionStart var a = edited.selectionStart
var b = edited.selectionEnd var b = edited.selectionEnd
// position 0: focus empty node above... // position 0: focus empty node above...
if(a == 0){ if(a == 0
&& edited.value.trim() != ''){
this.Block('prev') this.Block('prev')
this.edit('prev') this.edit('prev')
// focus new node... // focus new node...
@ -2480,6 +2524,7 @@ var Outline = {
setTimeout(function(){ setTimeout(function(){
that.focus() }, 0) } that.focus() }, 0) }
/*/ /*/
// XXX this for some reason takes lots of time at this point...
this.focus() } this.focus() }
//*/ //*/

View File

@ -48,12 +48,15 @@ var setup = function(){
- -
- ## Bugs: - ## Bugs:
focused:: true focused:: true
- BUG: deleting an element in edit mode breaks code (err)
- click the next line and press `backspace`
-
- BUG: navigation in edit mode broken... - BUG: navigation in edit mode broken...
- -
- // can't go past this down... - // can't go past this down...
- -
- BUG: caret positioning broken - BUG: caret positioning broken
- Example: - *TODO*::
- text text text - text text text
<div> <div>
block element block element
@ -72,30 +75,30 @@ 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)
- clicking right of the last line places cursor wrong - *DONE*::
- _this is a problem with the new version of `getMarkdownOffset(..)`_ collapsed:: true
- DONE text text text - text text text
- DONE text text text - text text text
text text text text text text
text text text text text text
- DONE M - M
M can't place cursor before first char M can't place cursor before first char
M M
- DONE text text text - text text text
``` ```
text text text text text text
``` ```
text text text text text text
- DONE text text text - text text text
_text text text_ _text text text_
text text text text text text
- DONE _text text text_ - _text text text_
text text text text text text
- DONE ``` - ```
text text text text text text
``` ```
text text text text text text
- DONE |text|text|text| - |text|text|text|
|text|text|text| |text|text|text|
|text|text|text| |text|text|text|
- BUG: parser: code blocks do not ignore single back-quotes... - BUG: parser: code blocks do not ignore single back-quotes...
@ -115,6 +118,7 @@ var setup = function(){
- side margins are a bit too large (account for toolbat to the right) - side margins are a bit too large (account for toolbat to the right)
- -
- ## ToDo: - ## ToDo:
- TOC?
- Q: should we use `HTMLTextAreaElement.autoUpdateSize(..)` or handle it globally in setup??? - Q: should we use `HTMLTextAreaElement.autoUpdateSize(..)` or handle it globally in setup???
- _...I'm leaning towards the later..._ - _...I'm leaning towards the later..._
- Q: can we place a cursor in a table correctly??? - Q: can we place a cursor in a table correctly???