Commit 27e48555 authored by techird's avatar techird

文本增强支持

parent 13691ec4
...@@ -22,17 +22,17 @@ define(function(require, exports, module) { ...@@ -22,17 +22,17 @@ define(function(require, exports, module) {
update: function(textGroup, node) { update: function(textGroup, node) {
function s(name) { function getDataOrStyle(name) {
return node.getData(name) || node.getStyle(name); return node.getData(name) || node.getStyle(name);
} }
var nodeText = node.getText() || ''; var nodeText = node.getText();
var textArr = nodeText.split('\n'); var textArr = nodeText ? nodeText.split('\n') : [' '];
var lineHeight = node.getStyle('line-height'); var lineHeight = node.getStyle('line-height');
var fontSize = s('font-size'); var fontSize = getDataOrStyle('font-size');
var fontFamily = s('font-family') || 'default'; var fontFamily = getDataOrStyle('font-family') || 'default';
var height = (lineHeight * fontSize) * textArr.length - (lineHeight - 1) * fontSize; var height = (lineHeight * fontSize) * textArr.length - (lineHeight - 1) * fontSize;
var yStart = -height / 2; var yStart = -height / 2;
...@@ -58,8 +58,8 @@ define(function(require, exports, module) { ...@@ -58,8 +58,8 @@ define(function(require, exports, module) {
textGroup.removeItem(i); textGroup.removeItem(i);
} }
} else if (textLength > textGroupLength) { } else if (textLength > textGroupLength) {
var length = textLength - textGroupLength; var growth = textLength - textGroupLength;
for (i = 0; i < length; i++) { while (growth--) {
textShape = new kity.Text() textShape = new kity.Text()
.setAttr('text-rendering', 'inherit'); .setAttr('text-rendering', 'inherit');
if (kity.Browser.ie) { if (kity.Browser.ie) {
...@@ -71,16 +71,15 @@ define(function(require, exports, module) { ...@@ -71,16 +71,15 @@ define(function(require, exports, module) {
} }
} }
for (i = 0, text, textShape; for (i = 0, text, textShape;
(text = textArr[i], textShape = textGroup.getItem(i)); i++) { (text = textArr[i], textShape = textGroup.getItem(i)); i++) {
textShape.setContent(text); textShape.node.innerHTML = text.replace('<', '&lt;').replace('>', '&gt;');
} }
this.setTextStyle(node, textGroup); this.setTextStyle(node, textGroup);
var textHash = node.getText() + var textHash = node.getText() +
[s('font-size'), s('font-name'), s('font-weight'), s('font-style')].join('/'); ['font-size', 'font-name', 'font-weight', 'font-style'].map(getDataOrStyle).join('/');
if (node._currentTextHash == textHash && node._currentTextGroupBox) return node._currentTextGroupBox; if (node._currentTextHash == textHash && node._currentTextGroupBox) return node._currentTextGroupBox;
...@@ -91,8 +90,8 @@ define(function(require, exports, module) { ...@@ -91,8 +90,8 @@ define(function(require, exports, module) {
var y = yStart + i * fontSize * lineHeight; var y = yStart + i * fontSize * lineHeight;
textShape.setY(y); textShape.setY(y);
var bbox = textShape.getBoundaryBox();
rBox = rBox.merge(new kity.Box(0, y, textShape.getBoundaryBox().width || 1, fontSize)); rBox = rBox.merge(new kity.Box(0, y, bbox.height && bbox.width || 1, fontSize));
}); });
var nBox = new kity.Box(r(rBox.x), r(rBox.y), r(rBox.width), r(rBox.height)); var nBox = new kity.Box(r(rBox.x), r(rBox.y), r(rBox.width), r(rBox.height));
...@@ -111,6 +110,25 @@ define(function(require, exports, module) { ...@@ -111,6 +110,25 @@ define(function(require, exports, module) {
} }
}); });
var TextCommand = kity.createClass({
base: Command,
execute: function(minder, text) {
var node = minder.getSelectedNode();
if (node) {
node.setText(text);
node.render();
minder.layout();
}
},
queryState: function(minder) {
return minder.getSelectedNodes().length == 1 ? 0 : -1;
},
queryValue: function(minder) {
var node = minder.getSelectedNode();
return node ? node.getText() : null;
}
});
utils.extend(TextRenderer, { utils.extend(TextRenderer, {
_styleHooks: [], _styleHooks: [],
...@@ -126,6 +144,9 @@ define(function(require, exports, module) { ...@@ -126,6 +144,9 @@ define(function(require, exports, module) {
}); });
Module.register('text', { Module.register('text', {
'commands': {
'text': TextCommand
},
'renderers': { 'renderers': {
center: TextRenderer center: TextRenderer
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment