无法在 Draft.js 中垂直调整图像大小

Hon*_*iao 1 javascript draftjs draft-js-plugins

我正在使用 Draft.js 插件Resizeable

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

但是,对于下面的代码,当我使用鼠标按图像的下边缘调整大小时,光标发生了变化,但无法调整大小。它仅在左侧和右侧边缘效果良好。

const resizeablePlugin = createResizeablePlugin({
  vertical: 'relative',
  horizontal: 'relative'
});
Run Code Online (Sandbox Code Playgroud)

codesandbox

看了源码,还是没明白是什么原因造成的。

Mik*_*kov 5

当您通过顶部或底部边缘调整大小时,该插件的开发人员似乎没有提供通过保存比率更改图像大小的机会。配置选项vertical: 'relative'意味着插件应该height以相对单位(百分比)设置值。您可以使用开发工具检查,当您尝试调整图像大小时,图像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;
  }
...
Run Code Online (Sandbox Code Playgroud)

我想你可以要求这个插件开发团队添加这个功能或者分叉这个项目。