diff --git a/experiments/outline-editor/editor.css b/experiments/outline-editor/editor.css index 60096ce..0056af5 100755 --- a/experiments/outline-editor/editor.css +++ b/experiments/outline-editor/editor.css @@ -15,6 +15,10 @@ position: relative; } +.editor .code { + display: none; +} + .editor .outline { display: block; position: relative; @@ -106,4 +110,14 @@ } +.editor .outline h1, +.editor .outline h2, +.editor .outline h3, +.editor .outline h4, +.editor .outline h5, +.editor .outline h6 { + margin-top: 0.5em; + margin-bottom: 0.5em; +} + diff --git a/experiments/outline-editor/editor.js b/experiments/outline-editor/editor.js index e866856..ba74189 100755 --- a/experiments/outline-editor/editor.js +++ b/experiments/outline-editor/editor.js @@ -74,8 +74,11 @@ var Outline = { // left_key_collapses: true, right_key_expands: true, + code_update_interval: 5000, + get code(){ + return this.dom.querySelector('.code') }, get outline(){ return this.dom.querySelector('.outline') }, get toolbar(){ @@ -273,8 +276,14 @@ var Outline = { next?.focus() return this }, + clear: function(){ + this.outline.innerText = '' + return this }, + // block serialization... - // XXX these should be symetrical... + // XXX STUB... + // XXX shouild we support headings + other formatting per block??? + // XXX these should be symetrical -- now one returns text the other an object... __code2html__: function(code){ var elem = { collapsed: false, @@ -287,13 +296,12 @@ var Outline = { elem.collapsed = value.trim() == 'true' return '' }) // markdown... - // XXX STUB... - .replace(/^#\s*(.*)\s*(\n|$)/, '

$1

') - .replace(/^##\s*(.*)\s*(\n|$)/, '

$1

') - .replace(/^###\s*(.*)\s*(\n|$)/, '

$1

') - .replace(/^####\s*(.*)\s*(\n|$)/, '

$1

') - .replace(/^#####\s*(.*)\s*(\n|$)/, '
$1
') .replace(/^######\s*(.*)\s*(\n|$)/, '
$1
') + .replace(/^#####\s*(.*)\s*(\n|$)/, '
$1
') + .replace(/^####\s*(.*)\s*(\n|$)/, '

$1

') + .replace(/^###\s*(.*)\s*(\n|$)/, '

$1

') + .replace(/^##\s*(.*)\s*(\n|$)/, '

$1

') + .replace(/^#\s*(.*)\s*(\n|$)/, '

$1

') .replace(/\*(.*)\*/g, '$1') .replace(/~([^~]*)~/g, '$1') .replace(/_([^_]*)_/g, '$1') @@ -301,16 +309,21 @@ var Outline = { .replace(/\n/g, '
\n') return elem }, __html2code__: function(html){ + var heading = function(level){ + return function(_, text, rest){ + return `${'#'.repeat(level)} ${text}${ + (rest != '' ? + '\n'+ rest + : '') }` } } return html - // XXX STUB... .replace(/
$/, '---') .replace(/
/, '---\n') - .replace(/^

(.*)<\/h1>\s*(.*)$/g, '# $1\n$2') - .replace(/^

(.*)<\/h2>\s*(.*)$/g, '## $1\n$2') - .replace(/^

(.*)<\/h3>\s*(.*)$/g, '### $1\n$2') - .replace(/^

(.*)<\/h4>\s*(.*)$/g, '#### $1\n$2') - .replace(/^

(.*)<\/h5>\s*(.*)$/g, '##### $1\n$2') - .replace(/^
(.*)<\/h6>\s*(.*)$/g, '###### $1\n$2') + .replace(/^
(.*)<\/h6>(.*)$/g, heading(6)) + .replace(/^
(.*)<\/h5>(.*)$/g, heading(5)) + .replace(/^

(.*)<\/h4>(.*)$/g, heading(4)) + .replace(/^

(.*)<\/h3>(.*)$/g, heading(3)) + .replace(/^

(.*)<\/h2>(.*)$/g, heading(2)) + .replace(/^

(.*)<\/h1>(.*)$/g, heading(1)) .replace(/(.*)<\/b>/g, '*$1*') .replace(/(.*)<\/s>/g, '~$1~') .replace(/(.*)<\/i>/g, '_$1_') @@ -339,21 +352,26 @@ var Outline = { node ??= this.json(node) indent ??= ' ' level ??= '' - var text = '' + var text = [] for(var elem of node){ - text += - level - +'- ' - + elem.text - .replace(/\n/g, '\n'+ level +' ') - +'\n' - + (elem.collapsed ? - level+' ' + 'collapsed:: true\n' - : '') - + this.text(elem.children || [], indent, level+indent) } - return text }, + text.push( + level +'- ' + + elem.text + .replace(/\n/g, '\n'+ level +' ') + + (elem.collapsed ? + '\n'+level+' ' + 'collapsed:: true' + : ''), + (elem.children + && elem.children.length > 0) ? + this.text(elem.children || [], indent, level+indent) + : [] ) } + return text + .flat() + .join('\n') }, parse: function(text){ + text = text + .replace(/^\s*\n/, '') text = ('\n' + text) .split(/\n(\s*)- /g) .slice(1) @@ -385,6 +403,7 @@ var Outline = { return parent } return level(text) }, + // XXX should this handle children??? // XXX revise name... Block: function(data={}, place=null){ if(typeof(data) != 'object'){ @@ -394,28 +413,44 @@ var Outline = { block.setAttribute('tabindex', '0') data.collapsed && block.setAttribute('collapsed', '') - var text - var html - block.append( - text = document.createElement('textarea') - .autoUpdateSize(), - html = document.createElement('span')) + var text = document.createElement('textarea') + var html = document.createElement('span') + block.append(text, html) if(data.text){ - text.value = data.text html.innerHTML = this.__code2html__ ? - this.__code2html__(data.text) - : data.text } + // NOTE: we are ignoring the .collapsed attr here (XXX) + this.__code2html__(data.text).text + : data.text + text.value = data.text + // XXX this does not see to work until we click in the textarea... + text.autoUpdateSize() } var cur = this.get() place && cur && cur[place](block) return block }, - // XXX use .__code2html__(..) load: function(data){ + var that = this data = typeof(data) == 'string' ? this.parse(data) : data // generate dom... - // XXX + var level = function(lst){ + return lst + .map(function(data){ + var elem = that.Block(data) + if((data.children || []).length > 0){ + elem.append(...level(data.children)) } + return elem }) } + this + .clear() + .outline + .append(...level(data)) + return this }, + + sync: function(){ + var code = this.code + code + && (code.innerHTML = this.text()) return this }, // XXX add scrollIntoView(..) to nav... @@ -578,6 +613,18 @@ var Outline = { && node.parentElement.setAttribute('collapsed', '') } else { node.nextElementSibling.innerHTML = node.value } } }) + // update .code... + var update_code_timeout + outline.addEventListener('change', + function(evt){ + if(update_code_timeout){ + return } + update_code_timeout = setTimeout( + function(){ + update_code_timeout = undefined + console.log('SYNC') + that.sync() }, + that.code_update_interval || 5000) }) // toolbar... var toolbar = this.toolbar @@ -598,6 +645,11 @@ var Outline = { // refocus the node after we are done... toolbar.addEventListener('click', refocusNode) } + // code... + var code = this.code + if(code){ + this.load(code.innerHTML) } + return this }, } diff --git a/experiments/outline-editor/index.html b/experiments/outline-editor/index.html index cccc765..c037e2d 100755 --- a/experiments/outline-editor/index.html +++ b/experiments/outline-editor/index.html @@ -24,35 +24,51 @@ var setup = function(){
-
-
- root -
- A -
a -
-
b -
-
c -
-
-
B -
d -
-
e -
-
-
C -
This is a line of text -
-
This is a set
- text lines
-
-
Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text -
-
-
-
+ +
+- # Outline editor prototype +- ## TODO + - editor: bksapce/del at start/end of a block should join it with prev/next + - editor: pressing enter in text edit mode should split text into two blocks + - editor: caret + - ~go to next/prev element's start/end when moving off last/first char~ + - handle up/down on wrapped blocks + _...can't seem to get caret line in a non-hacky way_ + - persistent empty first/last node (a button to create a new node) + - loading from DOM -- fill textarea + - ~focus management~ + - ~mouse/touch controls~ + - ~navigation~ + - ~expand/collapse subtree~ + - ~shift subtree up/down~ + - ~create node~ + - ~edit node~ + - shifting nodes up/down + - multiple node selection + - undo + - delete node + - indent/deindent + - edit node + - copy/paste nodes/trees + - ~serialize~/deserialize + - ~add optional text styling to nodes~ + - +- ## TEST + - A + collapsed:: true + - a + - b + - c + - B + - d + - e + - C + - This is a line of text + - This is a set + text lines + - Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text Lots of text
+ +
@@ -68,30 +84,6 @@ var setup = function(){
-TODO:
-- caret
-  - go to next/prev element's start/end when moving off last/first char
-  - handle up/down on wrapped blocks
-    ...can't seem to get caret line in a non-hacky way
-- persistent empty first/last node (a button to create a new node)
-- loading from DOM -- fill textarea
-- Firefox compatibility -- remove ':has(..)'
-- focus management
-- mouse/touch controls
-- navigation
-- expand/collapse subtree
-- shift subtree up/down
-- create node
-- edit node
-- shifting nodes up/down
-- multiple node selection
-- undo 
-  - delete node
-  - indent/deindent
-  - edit node
-- copy/paste nodes/trees
-- serialize/deserialize
-- add optional text styling to nodes
 
 Controls:
 	up         - focus node above