summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/services/utils.js
blob: e352fa8a9db69e5d95a23c4bd4fb3be1d2605a5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
export const hasSelection = (tiptapEditor) => {
  const { from, to } = tiptapEditor.state.selection;

  return from < to;
};

/**
 * Extracts filename from a URL
 *
 * @example
 *   > extractFilename('https://gitlab.com/images/logo-full.png')
 *   < 'logo-full'
 *
 * @param {string} src The URL to extract filename from
 * @returns {string}
 */
export const extractFilename = (src) => {
  return src.replace(/^.*\/|\.[^.]+?$/g, '');
};

export const readFileAsDataURL = (file) => {
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.addEventListener('load', (e) => resolve(e.target.result), { once: true });
    reader.readAsDataURL(file);
  });
};

export const clamp = (n, min, max) => Math.max(Math.min(n, max), min);