Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/format/insert-image.ts
1698 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Transforms, Element } from "slate";
7
import {
8
get_insert_image_opts_from_user,
9
Options,
10
} from "@cocalc/frontend/codemirror/extensions/insert-image";
11
import { alert_message } from "@cocalc/frontend/alerts";
12
import { getSelection } from "./commands";
13
14
export async function insertImage(editor): Promise<void> {
15
let opts: Options | undefined = undefined;
16
try {
17
opts = await get_insert_image_opts_from_user(
18
"In addition to inserting images using a URL here, you can both drag-and-drop and paste image files directly, and drag to resize images, rather than entering a width or height below and an external URL."
19
);
20
} catch (err) {
21
alert_message({ type: "error", message: err.errorFields[0]?.errors });
22
return;
23
}
24
if (opts == null) return; // user canceled.
25
26
const node = {
27
type: "image",
28
isInline: true,
29
isVoid: true,
30
src: opts.url,
31
title: opts.title,
32
height: opts.height,
33
width: opts.width,
34
children: [{ text: "" }],
35
} as Element;
36
Transforms.insertFragment(editor, [node], { at: getSelection(editor) });
37
}
38
39