Path: blob/master/src/packages/frontend/editors/slate/elements/hooks.ts
1698 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45export { useFocused, useSelected } from "../slate-react";67import {8useEffect,9useFrameContext,10useRef,11} from "@cocalc/frontend/app-framework";12import { Range } from "slate";13import { path_split } from "@cocalc/util/misc";14import { useSlateStatic as useSlateStatic0 } from "../slate-react";15import { SlateEditor } from "../editable-markdown";1617// Exactly like the normal useSlate hook, except return type is18// SlateEditor, which we know since we're only using this in CoCalc19// where we only use our enhanced type.20// NOTE: for elements *ONLY* useSlateStatic is actually provided,21// since useSlate would force every element that uses it to update22// on every editor change, which is no good.23export const useSlate = () => {24return useSlateStatic0() as SlateEditor;25};2627export const useSlateStatic = () => {28return useSlateStatic0() as SlateEditor;29};3031// Whether or not the current selection exists and is collapsed (i.e., not32// a range).33export const useCollapsed = () => {34const editor = useSlate();35return editor.selection != null && Range.isCollapsed(editor.selection);36};3738export const useProcessLinks = (39deps: (string | undefined)[],40{ doubleClick } = { doubleClick: false },41) => {42// TODO: implementation is very ugly!43const ref = useRef<any>(null);44const { project_id, path } = useFrameContext();45useEffect(() => {46if (ref.current == null) return;47const elt = $(ref.current);48(elt as any).process_smc_links({49project_id,50file_path: path_split(path).head, // TODO: inefficient to compute this every time.51doubleClick,52});53}, deps);54return ref;55};565758