question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resize images on editor

See original GitHub issue

Is your feature request related to a problem? Please describe. I would like to be able to resize images inside the editor. That way, content would be more flexible. I didn’t find a way to do this through tiptap.

Describe the solution you’d like Adding a resize option like this (gif bellow) would help so much!

Describe alternatives you’ve considered I really don’t know how to implement this. Should this be on the Image plugin or a totally separated plugin? If you guys give some ideas I could make a PR.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:49
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
philippkuehncommented, Aug 20, 2019

for now I don’t have plans to implement this in the core package

9reactions
theodorenguyen45commented, Mar 24, 2022

My workaround for this at the moment:

I created CustomResizableImage component:


<template>

  <node-view-wrapper as="span" class="image-container">

    <img

      v-bind="node.attrs"

      ref="resizableImg"

      :draggable="isDraggable"

      :data-drag-handle="isDraggable"

    />

    <v-icon

      class="ml-n3 resize-icon hidden"

      ref="icon"

      @mousedown="onMouseDown"

    >

      mdi-arrow-top-left-bottom-right-bold

    </v-icon>

  </node-view-wrapper>

</template>

<script>

import { NodeViewWrapper, nodeViewProps } from '@tiptap/vue-2';

export default {

  components: {

    NodeViewWrapper,

  },

  props: nodeViewProps,

  data: () => ({

    isResizing: false,

    lastMovement: {},

    count: 0,

  }),

  computed: {

    isDraggable() {

      return this.node?.attrs?.isDraggable;

    },

  },

  watch: {},

  mounted() {},

  methods: {

    onMouseDown(e) {

      e.preventDefault();

      this.isResizing = true;

      window.addEventListener('mousemove', this.throttle(this.onMouseMove));

      window.addEventListener('mouseup', this.onMouseUp);

    },

    onMouseUp(e) {

      // e.preventDefault();

      this.isResizing = false;

      this.lastMovement = {};

      window.removeEventListener('mousemove', this.throttle(this.onMouseMove));

      window.removeEventListener('mouseup', this.onMouseUp);

    },

    throttle(fn, wait = 60, leading = true) {

      let prev, timeout, lastargs;

      return (...args) => {

        lastargs = args;

        if (timeout) return;

        timeout = setTimeout(

          () => {

            timeout = null;

            prev = [Date.now](http://date.now/)();

            // let's do this ... we'll release the stored args as we pass them through

            fn.apply(this, lastargs.splice(0, lastargs.length));

            // some fancy timing logic to allow leading / sub-offset waiting periods

          },

          leading ? (prev && Math.max(0, wait - [Date.now](http://date.now/)() + prev)) || 0 : wait

        );

      };

    },

    onMouseMove(e) {

      e.preventDefault();

      if (!this.isResizing) {

        return;

      }

      if (!Object.keys(this.lastMovement).length) {

        this.lastMovement = { x: e.x, y: e.y };

        return;

      }

      if (e.x === this.lastMovement.x && e.y === this.lastMovement.y) {

        return;

      }

      let nextX = e.x - this.lastMovement.x;

      let nextY = e.y - this.lastMovement.y;

      const width = this.$refs.resizableImg.width + nextX;

      const height = this.$refs.resizableImg.height + nextY;

      this.lastMovement = { x: e.x, y: e.y };

      this.updateAttributes({ width, height });

    },

  },

};

</script>

<style lang="scss" scoped>

.image-container:hover {

  .hidden {

    visibility: visible !important;

  }

}

.image-container {

  overflow: hidden;

  position: relative;

}

.resize-icon {

  position: absolute;

  bottom: 0;

}

::v-deep.resize-icon {

  cursor: se-resize !important;

}

</style>

and rendered it in my CustomImage Extension

import Image from '@tiptap/extension-image';

import { VueNodeViewRenderer } from '@tiptap/vue-2';

import ResizableImageTemplate from './ResizableImageTemplate.vue';

const CustomImage = Image.extend({

	name: 'CustomImage',

	addAttributes() {

		// Return an object with attribute configuration

		return {

			...this.parent?.(),

			src: {

				default: '',

				renderHTML: attributes => {

					// … and return an object with HTML attributes.

					return {

						src: attributes.src,

					};

				},

			},

			width: {

				default: 750,

				renderHTML: ({ width }) => ({ width }),

			},

			height: {

				default: 500,

				renderHTML: ({ height }) => ({ height }),

			},

			isDraggable: {

				default: true,

				// We don't want it to render on the img tag

				renderHTML: attributes => {

					return {};

				},

			},

		};

	},

	addNodeView() {

		return VueNodeViewRenderer(ResizableImageTemplate);

	},

});

export { CustomImage };

export default CustomImage;

After that, just use this CustomImage instead of the original Image.

EDITED: Return src attribute instead of style

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resize images in editor - AnkiWeb
This add-on allows the user to resize an image with just a click and drag in the editor. Please visit the GitHub page...
Read more >
Free Online Image Resizer | Resize Photos Easily - BeFunky
Upload your photo into the Photo Editor, navigate to the Edit section, and then select Resize. You can get an in-depth look at...
Read more >
Free Image Resizer: Resize Photos Online | Adobe Express
Resize your photos easily with the Adobe Express free image resizer. Simply upload your pictures, change the photo size, and download your new...
Read more >
Resize image files online - Editor that allows ... - Img2Go.com
Change an image's size and file size with the Img2Go image resize tool. Resize image files for social media, uploading on the web,...
Read more >
PicResize - Crop, Resize, Edit images online for free!
The original FREE picture resize and crop tool since 2005! Resize, crop, compress, add effects to your images, photos, and screenshots for free!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found