You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2015/09/10 00:06:47 UTC
[4/8] allura git commit: [#7955] WIP on contextual code snippet button
[#7955] WIP on contextual code snippet button
Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/519a91a8
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/519a91a8
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/519a91a8
Branch: refs/heads/db/7955
Commit: 519a91a8de83e2331ea4fb41d445afdd09cd7c20
Parents: 424fad7
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Fri Sep 4 15:13:35 2015 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Fri Sep 4 15:13:35 2015 +0000
----------------------------------------------------------------------
.../lib/widgets/resources/js/sf_markitup.js | 78 ++++++++++++++++++++
1 file changed, 78 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/allura/blob/519a91a8/Allura/allura/lib/widgets/resources/js/sf_markitup.js
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/js/sf_markitup.js b/Allura/allura/lib/widgets/resources/js/sf_markitup.js
index 3331b0e..ec84972 100644
--- a/Allura/allura/lib/widgets/resources/js/sf_markitup.js
+++ b/Allura/allura/lib/widgets/resources/js/sf_markitup.js
@@ -30,6 +30,12 @@ $(window).load(function() {
// Override action for "preview" & "guide" tools
var toolbar = [
"bold", "italic", "heading", "|",
+ {
+ name: 'code',
+ action: toggleCodeBlockTildes,
+ className: 'fa fa-code',
+ title: 'Code Block'
+ },
"quote", "unordered-list", "ordered-list",
{
name: 'table',
@@ -85,6 +91,78 @@ $(window).load(function() {
'Content Cell | Content Cell | Content Cell ');
}
+ function toggleCodeBlockTildes(editor) {
+ var cm = editor.codemirror,
+ start = cm.getCursor('start'),
+ end = cm.getCursor('end'),
+ tok = cm.getTokenAt(start),
+ types = tok.type ? tok.type.split(' ') : [],
+ is_code = types.indexOf('comment') !== -1,
+ is_code_block_indented = tok.state.base.indentedCode,
+ is_code_single_line = tok.state.base.code,
+ is_code_block_fenced = !is_code_block_indented && !is_code_single_line;
+
+ console.log(tok);
+ console.log(cm.getLineTokens(start.line));
+
+ if (is_code) {
+ if (is_code_single_line) {
+ // similar to some SimpleMDE _toggleBlock logic
+ text = cm.getLine(start.line);
+ start = text.slice(0, start.ch);
+ end = text.slice(start.ch);
+ start = start.replace('`','');
+ end = end.replace('`', '');
+ cm.replaceRange(start + end, {
+ line: start.line,
+ ch: 0
+ }, {
+ line: start.line,
+ ch: 99999999999999
+ })
+ } else {
+ var block_start, block_end;
+ /* FIXME need a way to find the block start and end
+ getTokenTypeAt doesn't work for indented blocks since first char is just a space
+ getLineHandle(i).styles doesn't work since there are multiple styles per line.
+ or could it?
+
+ */
+ for (block_start = start.line; block_start >= 0; block_start--) {
+ types = cm.getTokenTypeAt({line: block_start, ch:1});
+ console.log(block_start, types);
+ if (types && types.split(' ').indexOf('comment') !== -1) {
+ block_start += 1;
+ break;
+ }
+ }
+ lineCount = cm.lineCount();
+ for (block_end = start.line; block_end < lineCount; block_end++) {
+ if (types && types.split(' ').indexOf('comment') !== -1) {
+ block_end -= 1;
+ break;
+ }
+ }
+ console.log(block_start, block_end);
+ if (is_code_block_indented) {
+ for (var line = block_start, end = block_end; line <= end; ++line) {
+ cm.indentLine(line, 'subtract');
+ }
+ } else if (is_code_block_fenced) {
+ cm.replaceRange('', {line: block_start-1, ch:0}, {line: block_start, ch:0});
+ cm.replaceRange('', {line: block_end, ch:0}, {line: block_end+1, ch:0});
+ }
+ }
+ } else {
+ if (start === end || start.line !== end.line) {
+ _replaceSelection(cm, false, '~~~~\n', '\n~~~~');
+ } else {
+ _replaceSelection(cm, false, '`', '`');
+ }
+
+ }
+ }
+
function show_help(editor) {
$help_contents.html('Loading...');
$.get($help_contents.attr('data-url'), function (data) {