Skip to content

Commit

Permalink
添加导入导出文本节点的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed May 1, 2015
1 parent 16e447e commit 2568d71
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 25 deletions.
27 changes: 14 additions & 13 deletions import.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{ path: 'src/core/readonly.js', pack: 'edit|share|m-share' },
{ path: 'src/core/layout.js', pack: 'edit|share|m-share' },
{ path: 'src/core/theme.js', pack: 'edit|share|m-share' },

{ path: 'src/core/compatibility.js', pack: 'edit|share|m-share' },
{ path: 'src/core/render.js', pack: 'edit|share|m-share' },
{ path: 'src/core/connect.js', pack: 'edit|share|m-share' },
Expand Down Expand Up @@ -229,6 +229,7 @@
{ path: 'ui/ribbon/appearence/style.js', pack: 'edit' },
{ path: 'ui/ribbon/appearence/font.js', pack: 'edit' },
{ path: 'ui/ribbon/appearence/color.js', pack: 'edit' },
{ path: 'ui/ribbon/appearence/slide.js', pack: 'edit' },

/* UI Ribbon「视图」面板 */
{ path: 'ui/ribbon/view/fullscreen.js', pack: 'edit' },
Expand All @@ -238,7 +239,7 @@

if (typeof(module) === 'object' && module.exports) {
module.exports = pathInfo;
}
}

else if (document) {

Expand All @@ -250,44 +251,44 @@
/***************************************************************************/
var scripts = document.getElementsByTagName('script');
document._currentScript = document.currentScript;

// return script object based off of src
var getScriptFromURL = function(url) {
for (var i = 0; i < scripts.length; i++)
if (scripts[i].src === url)
return scripts[i];

return undefined;
};

var actualScript = document.actualScript = function() {
if (document._currentScript)
return document._currentScript;

var stack;
try {
window.omgwtf();
} catch(e) {
stack = e.stack;
}

if (!stack)
return undefined;

var e = stack.indexOf(' at ') !== -1 ? ' at ' : '@';
while (stack.indexOf(e) !== -1)
stack = stack.substring(stack.indexOf(e) + e.length);

stack = stack.substring(stack.indexOf('http'), stack.indexOf(':', stack.indexOf(':')+1));

return getScriptFromURL(stack);
};
if (document.__defineGetter__)
document.__defineGetter__('currentScript', actualScript);

})();


}
/* jshint browser:true */
var script = document.currentScript || document.actualScript();
Expand All @@ -302,4 +303,4 @@
}
}
}
})();
})();
7 changes: 6 additions & 1 deletion lang/zh-cn/zh-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ KityMinder.LANG['zh-cn'] = {
'font': '字体',
'color': '颜色',
'background': '背景',
'slide': '演示',
'insert': '插入',
'arrange': '调整',
'nodeop': '当前',
Expand Down Expand Up @@ -156,6 +157,8 @@ KityMinder.LANG['zh-cn'] = {
'command': {
'appendsiblingnode': '插入同级主题',
'appendchildnode': '插入下级主题',
'importtextnode': '导入文本节点',
'exporttextnode': '导出文本节点',
'removenode': '删除',
'editnode': '编辑',
'arrangeup': '上移',
Expand Down Expand Up @@ -326,6 +329,8 @@ KityMinder.LANG['zh-cn'] = {
'removeimage': '移除已有图片',
'removenote': '移除已有备注',
'resetlayout': '整理',
'importtextnode': '导入文本节点',
'exporttextnode': '导出文本节点',

'justnow': '刚刚',
'minutesago': '{0} 分钟前',
Expand Down Expand Up @@ -408,4 +413,4 @@ KityMinder.LANG['zh-cn'] = {
'resource': {
'resource': '资源...'
}
};
};
179 changes: 177 additions & 2 deletions src/module/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ var EditNodeCommand = kity.createClass('EditNodeCommand', {
var selectedNode = km.getSelectedNode();
if (!selectedNode) {
return -1;
} else {
}
else {
return 0;
}
},
Expand All @@ -80,12 +81,182 @@ var EditNodeCommand = kity.createClass('EditNodeCommand', {
}
});

var $importDialog;
var $exportDialog;

var ImportTextNode = kity.createClass('ImportTextNode', {
base: Command,
execute: function(km, text) {
if (!$importDialog) {
var plainProtocol = km.getProtocol('plain');

var height = 400;
$importDialog = new FUI.Dialog({
width: 600,
height: height,
prompt: true,
caption: km.getLang('ui.importtextnode')
}).appendTo(document.getElementById('content-wrapper'));

var $dialogBody = $($importDialog.getBodyElement());

$dialogBody.html([
'<textarea style="width: 100%; height: ' + (height - 130) + 'px"></textarea>'
].join(''));

var $editor = $dialogBody.find('textarea');
var $ok = $importDialog.getButton(0);
var $errorMsg = $('<span class="validate-error"></span>');
var rootNode;

function check(value) {
var error;
if (!value) {
error = '内容不能为空';
}
else {
try {
rootNode = plainProtocol.decode(value, true);
}
catch (ex) {
error = '文本格式解析错误. ' + ex.message;
}
}

if (error) {
$editor.addClass('validate-error');
$errorMsg.text(error);
$ok.disable();
}
else {
$editor.removeClass('validate-error');
$errorMsg.text('');
$ok.enable();
}
}

$editor.after($errorMsg);

$editor.on('input', function() {
check($editor.val());
});
$editor.on('keydown keyup', function(e) {
if (e.keyCode === 9 && e.type === 'keydown') {
e.preventDefault();
var s = this.selectionStart;
this.value = this.value.substring(0, this.selectionStart) + '\t' +
this.value.substring(this.selectionEnd);
this.selectionEnd = s + 1;
}
if (e.keyCode === 13 && !e.shiftKey) {
return;
}
e.stopPropagation();
});
$importDialog.on('open', function() {
check($editor.val());
setTimeout(function() {
$editor[0].focus();
}, 10);
});
$importDialog.on('ok', function() {
var _selectedNodes = [];

function appendChild(parent, child, index) {
if (!parent || !child) {
return;
}
var kmNode = km.createNode(child.data.text, parent);
if (child.children) {
child.children.forEach(function(item) {
appendChild(kmNode, item);
});
}
kmNode.render();
return kmNode;
}

var selectedNode = km.getSelectedNode();
if (rootNode && rootNode.children && selectedNode) {
rootNode.children.forEach(function(child) {
_selectedNodes.push(appendChild(selectedNode, child));
});
}
km.select(_selectedNodes, true);
km.layout(300);

$editor.val('');
_selectedNodes = null;
rootNode = null;
});
}
$importDialog.open();
},
queryState: function(km) {
var selectedNode = km.getSelectedNode();
return selectedNode ? 0 : -1;
}
});

var ExportTextNode = kity.createClass('ExportTextNode', {
base: Command,
execute: function(km, text) {
if (!$exportDialog) {
var plainProtocol = km.getProtocol('plain');

var height = 400;
$exportDialog = new FUI.Dialog({
width: 600,
height: height,
prompt: true,
caption: km.getLang('ui.exporttextnode')
}).appendTo(document.getElementById('content-wrapper'));

var $dialogBody = $($exportDialog.getBodyElement());

$dialogBody.html([
'<textarea style="width: 100%; height: ' + (height - 130) + 'px"></textarea>'
].join(''));

var $editor = $dialogBody.find('textarea');

$editor.on('keydown keyup', function(e) {
if (e.keyCode === 9 && e.type === 'keydown') {
e.preventDefault();
var s = this.selectionStart;
this.value = this.value.substring(0, this.selectionStart) + '\t' +
this.value.substring(this.selectionEnd);
this.selectionEnd = s + 1;
}
if (e.keyCode === 13 && !e.shiftKey) {
return;
}
e.stopPropagation();
});

$exportDialog.on('open', function() {
$editor.val(plainProtocol.encode(km.getSelectedNode()));
setTimeout(function() {
$editor[0].focus();
}, 10);
});
}
$exportDialog.open();
},
queryState: function(km) {
var selectedNode = km.getSelectedNode();
return selectedNode ? 0 : -1;
}
});

KityMinder.registerModule('NodeModule', function() {
return {

commands: {
'AppendChildNode': AppendChildCommand,
'AppendSiblingNode': AppendSiblingCommand,
'ImportTextNode': ImportTextNode,
'ExportTextNode': ExportTextNode,
'RemoveNode': RemoveNodeCommand,
'EditNode': EditNodeCommand
},
Expand All @@ -98,6 +269,10 @@ KityMinder.registerModule('NodeModule', function() {
command: 'editnode'
}, {
command: 'removenode'
}, {
command: 'importtextnode'
}, {
command: 'exporttextnode'
}, {
divider: 1
}],
Expand All @@ -109,4 +284,4 @@ KityMinder.registerModule('NodeModule', function() {
'removenode': 'normal::Del|Backspace'
}
};
});
});
24 changes: 19 additions & 5 deletions src/protocol/plain.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ KityMinder.registerProtocol('plain', function(minder) {
};
}

function decode(local) {
/**
* 文本解码
*
* @param {string} local 文本内容
* @param {=boolean} root 自动根节点
* @return {Object} 返回解析后节点
*/
function decode(local, root) {
var json,
offset,
parentMap = {},
lines = local.split(LINE_ENDING_SPLITER),
line, level, node;
Expand All @@ -50,12 +58,18 @@ KityMinder.registerProtocol('plain', function(minder) {
var children = parent.children || (parent.children = []);
children.push(child);
}
if (root) {
parentMap[0] = json = getNode('root');
offset = 1;
} else {
offset = 0;
}

for (var i = 0; i < lines.length; i++) {
line = lines[i];
if (isEmpty(line)) continue;

level = getLevel(line);
level = getLevel(line) + offset;
node = getNode(line);

if (level === 0) {
Expand Down Expand Up @@ -84,10 +98,10 @@ KityMinder.registerProtocol('plain', function(minder) {
return encode(json, 0);
},

decode: function(local) {
return decode(local);
decode: function(local, root) {
return decode(local, root);
},

recognizePriority: -1
};
});
});
2 changes: 1 addition & 1 deletion ui/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,4 @@ KityMinder.registerUI('nav', function(minder) {
return $trigger;
}

});
});
2 changes: 1 addition & 1 deletion ui/ribbon/appearence/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ KityMinder.registerUI('ribbon/appearence/color', function(minder) {
color: $colorPanel,
background: $backgroundPanel
};
});
});
Loading

0 comments on commit 2568d71

Please sign in to comment.