SVG中的内联文本编辑

29
我在一个网站中呈现了内联SVG,并需要使用户能够以所见即所得的方式添加和修改SVG内的文本。基本上,我需要像svg-edit那样工作的东西。然而,我不需要完全的所见即所得编辑器,只需要内联文本编辑部分。我已经查看了svg-edit的源代码,似乎很难仅提取其中的部分。
因此,我正在寻找一种简单的方法(可能是通过第三方库)来实现内联SVG文本编辑。我已经考虑过在聚焦时将SVG文本替换为HTML文本输入,但在编辑模式下,文本必须与生成的SVG中呈现的文本完全相同。
4个回答

19

我做了一个小工具,可以在SVG中点击任意位置创建可编辑文本。最后一步是获取HTML文本并放入SVG元素中。

http://jsfiddle.net/brx3xm59/

以下是代码:

var mousedownonelement = false;

window.getlocalmousecoord = function (svg, evt) {
    var pt = svg.createSVGPoint();
    pt.x = evt.clientX;
    pt.y = evt.clientY;
    var localpoint = pt.matrixTransform(svg.getScreenCTM().inverse());
    localpoint.x = Math.round(localpoint.x);
    localpoint.y = Math.round(localpoint.y);
    return localpoint;
};

window.createtext = function (localpoint, svg) {
    var myforeign = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject')
    var textdiv = document.createElement("div");
    var textnode = document.createTextNode("Click to edit");
    textdiv.appendChild(textnode);
    textdiv.setAttribute("contentEditable", "true");
    textdiv.setAttribute("width", "auto");
    myforeign.setAttribute("width", "100%");
    myforeign.setAttribute("height", "100%");
    myforeign.classList.add("foreign"); //to make div fit text
    textdiv.classList.add("insideforeign"); //to make div fit text
    textdiv.addEventListener("mousedown", elementMousedown, false);
    myforeign.setAttributeNS(null, "transform", "translate(" + localpoint.x + " " + localpoint.y + ")");
    svg.appendChild(myforeign);
    myforeign.appendChild(textdiv);

};

function elementMousedown(evt) {
    mousedownonelement = true;
}


$(('#thesvg')).click(function (evt) {
    var svg = document.getElementById('thesvg');
    var localpoint = getlocalmousecoord(svg, evt);
    if (!mousedownonelement) {
        createtext(localpoint, svg);
    } else {
        mousedownonelement = false;
    }
});

2
我为此添加了一些功能,比如能够点击文本节点进行编辑,使用enter/esc键来接受/取消等。http://jsfiddle.net/gordonwoodhull/undr1kx6/44/ - Gordon
1
解决方案基于使用foreignObjecthttps://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject - setec

2
编辑: 更新示例以适用于Edge。客户端边界框的属性在那里不同 - 下面报告了旧IPads和Safari的问题。我已经在Edge、Chrome、FF、Safari(Mac)和Chrome、FF、Safari(iPad)上进行了测试。在Edge上,光标是破损的,但编辑仍然有效。

我知道这是一个老问题,但如果你不想实现自己的输入元素行为,contentEditable技巧仍然是我们所拥有的全部。如果你使用单个SVG文本节点(而不是HTML外部对象)作为文本编辑下方的覆盖层,你可以获得真正的WSYWIG,因为你可以使用与原始文本相同的字体等。你还可以选择哪些元素可以被编辑。是的,在Safari中光标很奇怪。这里可以找到演示这一点的小提琴:

https://jsfiddle.net/AaronDavidNewman/ta0jhw1q/

HTML/SVG:

<div id="yyy">

  <div id="xxx">

    <svg width="500" height="500" viewBox="0 0 500 500">
      <text x="0" y="25" id="target1" font-size="1.8em">Change me</text>
      <text x="0" y="50" id="targetx" font-size="1.8em">You can't edit me</text>
      <text x="0" y="75" id="target2" font-size="1.8em">Edit me</text>
      <text x="0" y="100" id="targety" font-size="1.8em">You can't edit me</text>
      <text x="0" y="125" id="target3" font-size="1.8em">Improve me</text>
    </svg>
  </div>
  <div id="aaa" contentEditable="true" class="hide">
    <svg width="500" height="50" viewBox="0 0 500 50">
      <text x="0" y="50" id="input-area" font-size="1.8em"></text>
    </svg>
  </div>
</div>

Javascript:

 // Create in-place editable text elements in svg.  Click inside the element 
// to edit it, and away to stop editing and switch to another element
var editing = false;
var svgns = "http://www.w3.org/2000/svg";
$('body').css('overflow','hidden');

// Poll on changes to input element.  A better approach might be 
// to update after keyboard events
var editElement = function(aaa, xxx) {
  setTimeout(function() {
    xxx.textContent = aaa.textContent;
    if (editing) {     
      editElement(aaa, xxx);
    }
  }, 250);
}

// Make sure the input svg element is placed directly over the 
// target element
var fixOffset = function(aaa, xxx) {
  var svg = $('#xxx').find('svg')[0];
  $('.underEdit').remove();
  var rect = xxx.getBoundingClientRect();
  var offset = aaa.getBoundingClientRect();
  $('#aaa').css('left', rect.left + (rect.left - offset.left));
  $('#aaa').css('top', rect.top + (rect.top - offset.top));
  var bb = xxx.getBBox();
  var margin = 10;
}
// Based on a click in the element svg area, edit that element
var editId = function(id) {
  var aaa = document.getElementById('input-area');
  var xxx = document.getElementById(id);
  var rect = xxx.getBoundingClientRect();
  $('#aaa').css('left', rect.left);
  $('#aaa').css('top', rect.top);
  setTimeout(function() {
    fixOffset(aaa, xxx);
  }, 1);
  aaa.textContent = xxx.textContent;
  editing = true;
  editElement(aaa, xxx);
}
// see if a click intersects an editable element
var getIntersection = function(objs, point) {
  var rv = null;
  $(objs).each(function(ix, obj) {
    var i1 = point.x - obj.box.x;
    var i2 = point.y - obj.box.y;
    // If inside the box, we have an element to edit
    if (i1 > 0 && i1 < obj.box.width && i2 > 0 && i2 < obj.box.height) {
      rv = obj;
      return false;
    } else if (i1 > -10 && i1 < obj.box.width + 10 && i2 > -10 && i2 < obj.box.height + 10) {
       // edit a nearby click, if a better match isn't found
      rv = obj;
    }
  });
  return rv;
}

// bind editable elements to mouse click
var bind = function(texts) {
  var objs = [];
  // find geometry of each editable element
  texts.forEach((id) => {
    var el = document.getElementById(id);
    var bbox = el.getBoundingClientRect();
    bbox = { x: bbox.left, y: bbox.top, width: bbox.width, height: bbox.height };
    objs.push({id: id, box: bbox });
  });
  // bind click event globally, then try to find the intersection.
  $(document).off('click').on('click', function(ev) {

    var point = {x: ev.clientX, y: ev.clientY };
    console.log('x:' + point.x + 'y:' + point.y);
    var obj = getIntersection(objs, point);
    if (obj && !editing) {
      $('#aaa').removeClass('hide');
      editing = true;
      console.log('start edit on ' + obj.id);
      editId(obj.id);
    } else if (!obj) {
      {
        $('#aaa').addClass('hide');
        editing = false;
        $('.underEdit').remove();
        console.log('stop editing');
      }
    }
  });
}

bind(['target1', 'target2', 'target3']);

CSS:

#yyy {
  position: relative;
  width: 500px;
  height: 500px;
}

#xxx {
  position: absolute;
  left: 100px;
  top: 100px;
  z-index: 1;
}

#aaa {
  position: absolute;
  left: 100px;
  top: 100px;
  z-index: 2;
  overflow:hidden;
}

.hide {
  display: none;
}

很遗憾,你的代码在我的iPad上无法运行。我可以选择文本,但是没有键盘显示,这使得编辑或创建文本变得不可能。 - ATL_DEV
@ATL_DEV,我在iPad上测试过了,软键盘和硬键盘都可以使用。你需要点击两次文本才能将其弹出。我使用的是iPad Pro 13.1.3 10.5英寸版本。唯一可能会出现问题的情况是你的屏幕太小了,这主要是针对jsfiddle而言。 - Aaron Newman

0
这是一个演示,可以编辑已经存在的文本(而不是创建新的文本条目): https://jsfiddle.net/qkrLy9gu 输入图像描述
<!DOCTYPE html>
<html>
<body>

<svg height="100" width="200">
    <circle cx="50" cy="50" r="40" fill="red"/>
    <text x="50" y="50" onclick="edittext(this)">a circle [click to edit]</text>
</svg> 

<script>
function edittext(svgtext){
    var input = document.createElement("input");
    input.value = svgtext.textContent
    input.onkeyup = function(e){    
        if (["Enter", "Escape"].includes(e.key)) {this.blur(); return};
        svgtext.textContent = this.value
    }
    input.onblur = function(e){       
        myforeign.remove()
    }

    var myforeign = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject')
    myforeign.setAttribute("width", "100%");
    myforeign.setAttribute("height", "100%");
    myforeign.append(input);
    
    svg = svgtext.parentNode
    svg.append(myforeign);

    input.focus()
}
</script>
 
</body>
</html>

0
这是一个例子,你可以从文本节点获取和更改文本。我建议编写一个JavaScript函数,用可编辑的
或类似的东西代替文本节点,在保存时使用
的innerHTML替换文本节点。
成功后请在此处发布最终代码。
<html>
  <head>
  <script>
    function svgMod(){ 
      var circle1 = document.getElementById("circle1"); 
      circle1.style.fill="blue";
    } 
    function svgMod2(){ 
      var circle1 = document.getElementById("circle1"); 
      t1.textContent="New content";
    } 
  </script>
  </head>
  <body>
  <svg id="svg1" xmlns="http://www.w3.org/2000/svg" style="width: 800; height: 1000">
  <circle id="circle1" r="100" cx="134" cy="134" style="fill: red; stroke: blue; stroke-width: 2"/>
  <text id="t1" x="50" y="120" onclick="alert(t1.textContent)">Example SVG text 1</text>
  </svg>
  <button onclick=circle1.style.fill="yellow";>Click to change to yellow</button>
  <button onclick=javascript:svgMod();>Click to change to blue</button>
  <button onclick=javascript:svgMod2();>Click to change text</button>
  </body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接