diff --git a/notebook.cs b/notebook.cs index 9c3d7cde..1ffe604a 100644 --- a/notebook.cs +++ b/notebook.cs @@ -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() @@ -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() @@ -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 @@ -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 @@ -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) @@ -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 = "" node.appendChild textNode(' \n') + source.parentNode.insertBefore node, source.nextSibling node codeSpan = (text, boxType)-> diff --git a/notebook.js b/notebook.js index 3c254483..cad1a7b1 100644 --- a/notebook.js +++ b/notebook.js @@ -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; @@ -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(); @@ -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(); @@ -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); } }; @@ -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; @@ -227,7 +235,7 @@ }; }; - exprBox = function exprBox(source) { + makeOutputBox = function makeOutputBox(source) { var node; node = document.createElement('div'); node.setAttribute('LeisureOutput', ''); @@ -235,8 +243,10 @@ node.setAttribute('class', 'output'); node.setAttribute('contentEditable', 'false'); node.source = source; + source.output = node; node.innerHTML = ""; node.appendChild(textNode(' \n')); + source.parentNode.insertBefore(node, source.nextSibling); return node; };