Created
May 30, 2014 09:46
-
-
Save dohzya/b757d90bf9ed488ac84f to your computer and use it in GitHub Desktop.
Test of folding a specific just-created node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function (require, exports, module) { | |
'use strict'; | |
var Extensions = require('ft/core/extensions').Extensions; | |
function updateTree(tree, fn) { | |
try { | |
tree.beginUpdates(); | |
fn(); | |
} finally { | |
tree.endUpdates(); | |
} | |
} | |
function prependChild(node, child) { | |
if (node.firstChild) { | |
node.insertChildBefore(child, node.firstChild); | |
} else { | |
node.appendChild(child); | |
} | |
} | |
function test(editor, options, tagName) { | |
var addTag; | |
function fun(node) { | |
var infos; | |
if (node.firstChild && node.firstChild.line().trim() == ":INFOS:") { | |
infos = node.firstChild; | |
} else { | |
infos = node.tree.createNode(":INFOS:"); | |
prependChild(node, infos); | |
} | |
var child = infos.tree.createNode("ADDED LINE"); | |
prependChild(infos, child); | |
editor.collapseNode(infos); | |
} | |
if (options && options.node) { | |
updateTree(node.tree, function () { | |
fun(node); | |
}); | |
} else { | |
var range = editor.selectedRange(); | |
updateTree(editor.tree(), function () { | |
range.forEachNodeInRange(fun); | |
}); | |
} | |
} | |
Extensions.addCommand({ | |
name: 'test', | |
description: 'Test', | |
performCommand: function (editor, options) { | |
test(editor, options); | |
} | |
}); | |
Extensions.addInit(function (editor) { | |
editor.addKeyMap({ | |
'Cmd-Alt-D' : 'test' | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- blah | |
- item | |
- blah | |
Put your cursor on `item` then press Cmd-Alt-D | |
I used `editor.collapseNode(infos);` at line 36, which is not in the API :-/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment