1

我正在使用 Draft.js 插件Resizeable

我正在尝试使用原始长宽比调整图像大小。

但是,对于下面的代码,当我使用鼠标通过图像的底部边缘调整大小时,光标发生了变化,但无法调整大小。它只适用于左右边缘。

const resizeablePlugin = createResizeablePlugin({
  vertical: 'relative',
  horizontal: 'relative'
});

codesandbox

查看源代码后,我仍然没有弄清楚是什么原因造成的。

4

1 回答 1

4

当您通过顶部或底部边缘调整大小时,该插件的开发人员似乎没有提供这个机会来改变图像大小和保存率。配置选项vertical: 'relative'意味着插件应该height以相对单位(百分比)设置值。您可以使用 devtools 检查当您尝试调整图像大小时是否height会发生变化。但是我们应该改变width值以达到当我们用保存率调整图像大小时的行为。

可以通过稍微改写源代码来实现。检查你的沙箱这个分支

检查createDecorator.js它是否与存储在/node_modules/draft-js-resizeable-plugin/lib/createDecorator.js. 我改变了什么?查找doDrag功能(我使用// !添加或更改的所有字符串进行营销):

...
var startWidth = parseInt(document.defaultView.getComputedStyle(pane).width, 10);
var startHeight = parseInt(document.defaultView.getComputedStyle(pane).height, 10);

var imageRect = pane.getBoundingClientRect(); // !
var imageRatio = imageRect.width / imageRect.height; // ! get image ratio

// Do the actual drag operation
var doDrag = function doDrag(dragEvent) {
  var width = startWidth + dragEvent.clientX - startX;
  var height = startHeight + dragEvent.clientY - startY;
  var block = store.getEditorRef().refs.editor;
  width = block.clientWidth < width ? block.clientWidth : width;
  height = block.clientHeight < height ? block.clientHeight : height;

  var widthForPercCalculation = (isTop || isBottom) && vertical === 'relative' ? height * imageRatio : width; // ! calculate new width value in percents

  var widthPerc = 100 / block.clientWidth * widthForPercCalculation; // !
  var heightPerc = 100 / block.clientHeight * height;

  var newState = {};
  if ((isLeft || isRight) && horizontal === 'relative') {
    newState.width = resizeSteps ? round(widthPerc, resizeSteps) : widthPerc;
  } else if ((isLeft || isRight) && horizontal === 'absolute') {
    newState.width = resizeSteps ? round(width, resizeSteps) : width;
  }

  if ((isTop || isBottom) && vertical === 'relative') {
    newState.width = resizeSteps ? round(widthPerc, resizeSteps) : widthPerc; // ! here we update width not height value
  } else if ((isTop || isBottom) && vertical === 'absolute') {
    newState.height = resizeSteps ? round(height, resizeSteps) : height;
  }
...

我认为您可以要求这个插件开发团队添加此功能或分叉项目。

于 2017-11-25T11:46:18.810 回答