Skip to content

Commit

Permalink
tied expr boxes to output boxes, so when they convert to and from, th…
Browse files Browse the repository at this point in the history
…e output boxes go with them
  • Loading branch information
zot committed Apr 23, 2012
1 parent fb57e61 commit e5ef5a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
31 changes: 18 additions & 13 deletions notebook.cs
Expand Up @@ -36,6 +36,7 @@
sp.setAttribute('generatedNL', '')
bx = box s.getRangeAt(0), 'codeMainExpr', true
bx.appendChild sp
makeOutputBox bx
r = document.createRange()
r.setStart(sp, 0)
s.removeAllRanges()
Expand All @@ -49,12 +50,18 @@ bx.appendChild sp
checkMutateFromModification = (b)->
if b?
inDef = selInDef()
if inDef and b.classList.contains('codeMainExpr')
b.classList.remove 'codeMainExpr'
b.classList.add 'codeMain'
else if !inDef and b.classList.contains('codeMain')
b.classList.remove 'codeMain'
b.classList.add 'codeMainExpr'
if inDef and b.classList.contains('codeMainExpr') then toDefBox b
else if !inDef and b.classList.contains('codeMain') then toExprBox b

toExprBox = (b)->
b.classList.remove 'codeMain'
b.classList.add 'codeMainExpr'
makeOutputBox b

toDefBox = (b)->
if b.output then b.parentNode.removeChild b.output
b.classList.remove 'codeMainExpr'
b.classList.add 'codeMain'

selInDef = (expectedClass)->
s = window.getSelection()
Expand All @@ -72,8 +79,7 @@ bx.appendChild sp
s = window.getSelection()
r = s.getRangeAt(0)
if p = selInDef('codeMainExpr')
p.classList.remove 'codeMainExpr'
p.classList.add 'codeMain'
toDefBox p

initNotebook = (el)->
el.replacing = true
Expand All @@ -83,9 +89,6 @@ removeOldDefs el
el.appendChild textNode('\n')
el.appendChild textNode('\n')
el.appendChild textNode('\n')
# bx = codeBox 'codeMain'
# bx.appendChild textNode('\n')
# el.appendChild bx
el.normalize()
el.replacing = false
pgm
Expand Down Expand Up @@ -130,7 +133,7 @@ s.appendChild textNode('\n')
s.setAttribute('generatedNL', '')
bx = box main, 'codeMainExpr', true
bx.appendChild s
bx.parentNode.insertBefore exprBox(bx), bx.nextSibling
makeOutputBox(bx)
pgm

textNode = (text)-> document.createTextNode(text)
Expand All @@ -147,15 +150,17 @@ bx.appendChild s
exBox.lastChild.scrollIntoView()
prompt:(msg, cont)-> cont(window.prompt(msg))

exprBox = (source)->
makeOutputBox = (source)->
node = document.createElement 'div'
node.setAttribute 'LeisureOutput', ''
node.setAttribute 'Leisure', ''
node.setAttribute 'class', 'output'
node.setAttribute 'contentEditable', 'false'
node.source = source
source.output = node
node.innerHTML = "<button onclick='Notebook.evalOutput(this.parentNode)'>-&gt;</button>"
node.appendChild textNode(' \n')
source.parentNode.insertBefore node, source.nextSibling
node

codeSpan = (text, boxType)->
Expand Down
32 changes: 21 additions & 11 deletions notebook.js
Expand Up @@ -4,7 +4,7 @@
*/

(function() {
var Leisure, ReplCore, addsLine, bindNotebook, box, checkMutateFromModification, checkMutateToDef, codeBox, codeSpan, continueRangePosition, delay, envFor, evalOutput, exprBox, findDefs, getBox, getRangePosition, getRanges, grp, initNotebook, makeRange, markupDefs, nodeEnd, prepExpr, removeOldDefs, root, selInDef, textNode;
var Leisure, ReplCore, addsLine, bindNotebook, box, checkMutateFromModification, checkMutateToDef, codeBox, codeSpan, continueRangePosition, delay, envFor, evalOutput, findDefs, getBox, getRangePosition, getRanges, grp, initNotebook, makeOutputBox, makeRange, markupDefs, nodeEnd, prepExpr, removeOldDefs, root, selInDef, textNode, toDefBox, toExprBox;

if ((typeof window !== "undefined" && window !== null) && (!(typeof global !== "undefined" && global !== null) || global === window)) {
window.global = window;
Expand Down Expand Up @@ -57,6 +57,7 @@
sp.setAttribute('generatedNL', '');
bx = box(s.getRangeAt(0), 'codeMainExpr', true);
bx.appendChild(sp);
makeOutputBox(bx);
r = document.createRange();
r.setStart(sp, 0);
s.removeAllRanges();
Expand All @@ -79,15 +80,25 @@
if (b != null) {
inDef = selInDef();
if (inDef && b.classList.contains('codeMainExpr')) {
b.classList.remove('codeMainExpr');
return b.classList.add('codeMain');
return toDefBox(b);
} else if (!inDef && b.classList.contains('codeMain')) {
b.classList.remove('codeMain');
return b.classList.add('codeMainExpr');
return toExprBox(b);
}
}
};

toExprBox = function toExprBox(b) {
b.classList.remove('codeMain');
b.classList.add('codeMainExpr');
return makeOutputBox(b);
};

toDefBox = function toDefBox(b) {
if (b.output) b.parentNode.removeChild(b.output);
b.classList.remove('codeMainExpr');
return b.classList.add('codeMain');
};

selInDef = function selInDef(expectedClass) {
var box, defType, leading, m, matched, name, r, s, txt;
s = window.getSelection();
Expand All @@ -109,10 +120,7 @@
if (!el.replacing) {
s = window.getSelection();
r = s.getRangeAt(0);
if (p = selInDef('codeMainExpr')) {
p.classList.remove('codeMainExpr');
return p.classList.add('codeMain');
}
if (p = selInDef('codeMainExpr')) return toDefBox(p);
}
};

Expand Down Expand Up @@ -193,7 +201,7 @@
s.setAttribute('generatedNL', '');
bx = box(main, 'codeMainExpr', true);
bx.appendChild(s);
bx.parentNode.insertBefore(exprBox(bx), bx.nextSibling);
makeOutputBox(bx);
}
}
return pgm;
Expand Down Expand Up @@ -227,16 +235,18 @@
};
};

exprBox = function exprBox(source) {
makeOutputBox = function makeOutputBox(source) {
var node;
node = document.createElement('div');
node.setAttribute('LeisureOutput', '');
node.setAttribute('Leisure', '');
node.setAttribute('class', 'output');
node.setAttribute('contentEditable', 'false');
node.source = source;
source.output = node;
node.innerHTML = "<button onclick='Notebook.evalOutput(this.parentNode)'>-&gt;</button>";
node.appendChild(textNode(' \n'));
source.parentNode.insertBefore(node, source.nextSibling);
return node;
};

Expand Down

0 comments on commit e5ef5a3

Please sign in to comment.