File size: 1,687 Bytes
60f878e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { CustomElement, LinkElement } from '@plait/common';
import { CustomEditor, RenderElementPropsFor } from '../custom-types';
import { isUrl, LinkEditor } from '@plait/text-plugins';

// Put this at the start and end of an inline component to work around this Chromium bug:
// https://bugs.chromium.org/p/chromium/issues/detail?id=1249405
export const InlineChromiumBugfix = () => (
  <span contentEditable={false} style={{ fontSize: 0 }}>

    {String.fromCodePoint(160) /* Non-breaking space */}

  </span>
);

export const LinkComponent = ({

  attributes,

  children,

  element,

}: RenderElementPropsFor<LinkElement>) => {
  return (
    <a

      {...attributes}

      style={{

        textDecoration: 'none',

        cursor: 'inherit',

      }}

      data-url={element.url}

      className="plait-board-link"

    >

      <InlineChromiumBugfix />

      {children}

      <InlineChromiumBugfix />

    </a>
  );
};

export const withInlineLink = (editor: CustomEditor) => {
  const { insertData, insertText, isInline } = editor;

  editor.isInline = (element: CustomElement) => {
    return (
      ((element as LinkElement).type &&
        ['link'].includes((element as LinkElement).type)) ||
      isInline(element)
    );
  };

  editor.insertText = (text) => {
    if (text && isUrl(text)) {
      LinkEditor.wrapLink(editor, text, text);
    } else {
      insertText(text);
    }
  };

  editor.insertData = (data) => {
    const text = data.getData('text/plain');

    if (text && isUrl(text)) {
      LinkEditor.wrapLink(editor, text, text);
    } else {
      insertData(data);
    }
  };

  return editor;
};