Path: blob/master/src/packages/frontend/editors/slate/format/insert-image.ts
1698 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Transforms, Element } from "slate";6import {7get_insert_image_opts_from_user,8Options,9} from "@cocalc/frontend/codemirror/extensions/insert-image";10import { alert_message } from "@cocalc/frontend/alerts";11import { getSelection } from "./commands";1213export async function insertImage(editor): Promise<void> {14let opts: Options | undefined = undefined;15try {16opts = await get_insert_image_opts_from_user(17"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."18);19} catch (err) {20alert_message({ type: "error", message: err.errorFields[0]?.errors });21return;22}23if (opts == null) return; // user canceled.2425const node = {26type: "image",27isInline: true,28isVoid: true,29src: opts.url,30title: opts.title,31height: opts.height,32width: opts.width,33children: [{ text: "" }],34} as Element;35Transforms.insertFragment(editor, [node], { at: getSelection(editor) });36}373839