Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/hooks.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
export { useFocused, useSelected } from "../slate-react";
7
8
import {
9
useEffect,
10
useFrameContext,
11
useRef,
12
} from "@cocalc/frontend/app-framework";
13
import { Range } from "slate";
14
import { path_split } from "@cocalc/util/misc";
15
import { useSlateStatic as useSlateStatic0 } from "../slate-react";
16
import { SlateEditor } from "../editable-markdown";
17
18
// Exactly like the normal useSlate hook, except return type is
19
// SlateEditor, which we know since we're only using this in CoCalc
20
// where we only use our enhanced type.
21
// NOTE: for elements *ONLY* useSlateStatic is actually provided,
22
// since useSlate would force every element that uses it to update
23
// on every editor change, which is no good.
24
export const useSlate = () => {
25
return useSlateStatic0() as SlateEditor;
26
};
27
28
export const useSlateStatic = () => {
29
return useSlateStatic0() as SlateEditor;
30
};
31
32
// Whether or not the current selection exists and is collapsed (i.e., not
33
// a range).
34
export const useCollapsed = () => {
35
const editor = useSlate();
36
return editor.selection != null && Range.isCollapsed(editor.selection);
37
};
38
39
export const useProcessLinks = (
40
deps: (string | undefined)[],
41
{ doubleClick } = { doubleClick: false },
42
) => {
43
// TODO: implementation is very ugly!
44
const ref = useRef<any>(null);
45
const { project_id, path } = useFrameContext();
46
useEffect(() => {
47
if (ref.current == null) return;
48
const elt = $(ref.current);
49
(elt as any).process_smc_links({
50
project_id,
51
file_path: path_split(path).head, // TODO: inefficient to compute this every time.
52
doubleClick,
53
});
54
}, deps);
55
return ref;
56
};
57
58