reworking attrs (not done yet)

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2023-10-22 13:55:41 +03:00
parent fb5dd628de
commit 3e5a3d7df7
2 changed files with 108 additions and 33 deletions

View File

@ -804,31 +804,6 @@ var Outline = {
elem?.focus() elem?.focus()
return elem }, return elem },
update: function(node='focused', data){
var node = this.get(node)
data ??= this.data(node, false)
typeof(data.collapsed) == 'boolean'
&& (data.collapsed ?
node.setAttribute('collapsed', '')
: node.removeAttribute('collapsed'))
if(data.text != null){
var text = node.querySelector('textarea')
var html = node.querySelector('span')
if(this.__code2html__){
// NOTE: we are ignoring the .collapsed attr here
var parsed = this.__code2html__(data.text)
html.innerHTML = parsed.text
// heading...
node.classList.remove(...this.__styles)
parsed.style
&& node.classList.add(...parsed.style)
} else {
html.innerHTML = data.text }
text.value = data.text
// XXX this does not see to work until we click in the textarea...
text.autoUpdateSize() }
return node },
// This will prevent spamming the .sync() by limiting calls to one // This will prevent spamming the .sync() by limiting calls to one
// per .change_interval // per .change_interval
// //
@ -855,6 +830,55 @@ var Outline = {
that.change_interval || 1000) that.change_interval || 1000)
return this }, return this },
__block_attrs__: {
id: 'attr',
collapsed: 'attr',
focused: 'cls',
},
update: function(node='focused', data){
var node = this.get(node)
data ??= this.data(node, false)
for(var [attr, value] of Object.entries(data)){
if(attr == 'children'){
continue }
if(attr == 'text'){
var text = node.querySelector('textarea')
var html = node.querySelector('span')
if(this.__code2html__){
// NOTE: we are ignoring the .collapsed attr here
var parsed = this.__code2html__(data.text)
html.innerHTML = parsed.text
// heading...
node.classList.remove(...this.__styles)
parsed.style
&& node.classList.add(...parsed.style)
} else {
html.innerHTML = data.text }
text.value = data.text
// XXX this does not seem to work until we click in the textarea...
text.autoUpdateSize()
continue }
var type = this.__block_attrs__[attr]
if(type == 'cls'){
value ?
node.classList.add(attr)
: node.classList.remove(attr)
} else if(type == 'attr'
|| type == undefined){
typeof(value) == 'boolean'?
(value ?
node.setAttribute(attr, '')
: node.removeAttribute(attr))
: (attr in data
&& value != null) ?
node.setAttribute(attr, value)
: node.removeAttribute(attr) } }
this.__change__()
return node },
// edit... // edit...
indent: function(node='focused', indent=true){ indent: function(node='focused', indent=true){
// .indent(<indent>) // .indent(<indent>)
@ -1030,9 +1054,23 @@ var Outline = {
// serialization... // serialization...
data: function(elem, deep=true){ data: function(elem, deep=true){
elem = this.get(elem) elem = this.get(elem)
// XXX move these to config...
var attrs = this.__block_attrs__
var cls_attrs = ['focused']
return { return {
text: elem.querySelector('textarea').value, text: elem.querySelector('textarea').value,
collapsed: elem.getAttribute('collapsed') != null, ...(Object.entries(attrs)
.reduce(function(res, [attr, type]){
if(type == 'attr'){
var val = elem.getAttribute(attr)
if(val != null){
res[attr] = val == '' ?
true
: val } }
if(type == 'cls'){
elem.classList.contains(attr)
&& (res[attr] = true) }
return res }, {})),
...(deep ? ...(deep ?
{children: this.json(elem)} {children: this.json(elem)}
: {}), : {}),
@ -1059,9 +1097,16 @@ var Outline = {
level +'- ' level +'- '
+ this.__code2text__(elem.text) + this.__code2text__(elem.text)
.replace(/\n/g, '\n'+ level +' ') .replace(/\n/g, '\n'+ level +' ')
+ (elem.collapsed ? // attrs...
'\n'+level+' ' + 'collapsed:: true' + (Object.keys(elem)
: ''), .reduce(function(res, attr){
return (attr == 'text'
|| attr == 'children') ?
res
: res
+ (elem[attr] ?
'\n'+level+' ' + `${ attr }:: ${ elem[attr] }`
: '') }, '')),
(elem.children (elem.children
&& elem.children.length > 0) ? && elem.children.length > 0) ?
this.text(elem.children || [], indent, level+indent) this.text(elem.children || [], indent, level+indent)
@ -1089,18 +1134,30 @@ var Outline = {
// same level... // same level...
if(sep.length == prev_sep.length){ if(sep.length == prev_sep.length){
var [_, block] = lst.splice(0, 2) var [_, block] = lst.splice(0, 2)
var collapsed = false var attrs = {
collapsed: false,
focused: false,
}
block = block block = block
// XXX generalize attr processing...
.replace(/\n\s*id::\s*(.*)\s*$/,
function(_, value){
attrs.id = value
return '' })
.replace(/\n\s*focused::\s*(.*)\s*$/,
function(_, value){
attrs.focused = value == 'true'
return '' })
.replace(/\n\s*collapsed::\s*(.*)\s*$/, .replace(/\n\s*collapsed::\s*(.*)\s*$/,
function(_, value){ function(_, value){
collapsed = value == 'true' attrs.collapsed = value == 'true'
return '' }) return '' })
parent.push({ parent.push({
text: that.__text2code__(block text: that.__text2code__(block
// normalize indent... // normalize indent...
.split(new RegExp('\n'+sep+' ', 'g')) .split(new RegExp('\n'+sep+' ', 'g'))
.join('\n')), .join('\n')),
collapsed, ...attrs,
children: [], children: [],
}) })
// indent... // indent...
@ -1514,7 +1571,7 @@ var Outline = {
// update .code... // update .code...
outline.addEventListener('change', outline.addEventListener('change',
function(evt){ function(evt){
this.__change__() }) that.__change__() })
// toolbar... // toolbar...
var toolbar = this.toolbar var toolbar = this.toolbar

View File

@ -47,6 +47,9 @@ var setup = function(){
- BUG: editor: FF seems to update the style every other key press -- should be live... - BUG: editor: FF seems to update the style every other key press -- should be live...
- -
- ## ToDo: - ## ToDo:
- ASAP: attrs in editor are not parsed correctly (see: [attrs](#attributes))
- ASAP: multiple attrs are not handled correctly (see: [attrs](#attributes))
- ASAP: style attrs (see: [attrs](#attributes))
- ASAP: scroll into view is bad... - ASAP: scroll into view is bad...
- ASAP: mobile browsers behave quite chaotically ignoring parts of the styling... - ASAP: mobile browsers behave quite chaotically ignoring parts of the styling...
- pgup/pgdown/home/end buttons - pgup/pgdown/home/end buttons
@ -266,6 +269,21 @@ var setup = function(){
- Symbols -- _should these be ligatures?_ - Symbols -- _should these be ligatures?_
- (i), (c), /!\, ... - (i), (c), /!\, ...
- -- and --- - -- and ---
- Attributes:
id:: attributes
- collapsed
collapsed:: true
- a
- b
- c
- id
id:: node-with-id
- combined
id:: combined-several-ids
collapsed:: true
- a
- b
- c
- -
- --- - ---
- ### Playground for testing - ### Playground for testing