-
Notifications
You must be signed in to change notification settings - Fork 35
/
link_editor.ts
198 lines (181 loc) · 4.93 KB
/
link_editor.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { Component, onMounted, useRef, useState } from "@odoo/owl";
import { markdownLink } from "../../../helpers";
import { detectLink, urlRepresentation } from "../../../helpers/links";
import { canonicalizeNumberContent } from "../../../helpers/locale";
import { linkMenuRegistry } from "../../../registries/menus/link_menu_registry";
import { DOMCoordinates, Link, Position, SpreadsheetChildEnv } from "../../../types";
import { CellPopoverComponent, PopoverBuilders } from "../../../types/cell_popovers";
import { css } from "../../helpers/css";
import { useAbsoluteBoundingRect } from "../../helpers/position_hook";
import { Menu } from "../../menu/menu";
const MENU_OFFSET_X = 320;
const MENU_OFFSET_Y = 100;
const PADDING = 12;
const LINK_EDITOR_WIDTH = 340;
const LINK_EDITOR_HEIGHT = 165;
css/* scss */ `
.o-link-editor {
font-size: 13px;
background-color: white;
box-shadow: 0 1px 4px 3px rgba(60, 64, 67, 0.15);
padding: ${PADDING}px;
display: flex;
flex-direction: column;
border-radius: 4px;
height: ${LINK_EDITOR_HEIGHT}px;
width: ${LINK_EDITOR_WIDTH}px;
.o-section {
.o-section-title {
font-weight: bold;
color: dimgrey;
margin-bottom: 5px;
}
}
.o-buttons {
padding-left: 16px;
padding-top: 16px;
padding-bottom: 16px;
text-align: right;
}
input {
box-sizing: border-box;
width: 100%;
border-radius: 4px;
padding: 4px 23px 4px 10px;
border: none;
height: 24px;
border: 1px solid lightgrey;
}
.o-link-url {
position: relative;
flex-grow: 1;
button {
position: absolute;
right: 0px;
top: 0px;
border: none;
height: 20px;
width: 20px;
background-color: #fff;
margin: 2px 3px 1px 0px;
padding: 0px 1px 0px 0px;
}
button:hover {
cursor: pointer;
}
}
}
`;
interface LinkEditorProps {
cellPosition: Position;
onClosed?: () => void;
}
interface State {
label: string;
url: string;
isUrlEditable: boolean;
}
export class LinkEditor extends Component<LinkEditorProps, SpreadsheetChildEnv> {
static template = "o-spreadsheet-LinkEditor";
static props = {
cellPosition: Object,
onClosed: { type: Function, optional: true },
};
static components = { Menu };
menuItems = linkMenuRegistry.getMenuItems();
private link: State = useState(this.defaultState);
private menu = useState({
isOpen: false,
});
private linkEditorRef = useRef("linkEditor");
private position: DOMCoordinates = useAbsoluteBoundingRect(this.linkEditorRef);
urlInput = useRef("urlInput");
setup() {
onMounted(() => this.urlInput.el?.focus());
}
get defaultState(): State {
const { col, row } = this.props.cellPosition;
const sheetId = this.env.model.getters.getActiveSheetId();
const cell = this.env.model.getters.getEvaluatedCell({ sheetId, col, row });
if (cell.link) {
return {
url: cell.link.url,
label: cell.formattedValue,
isUrlEditable: cell.link.isUrlEditable,
};
}
return {
label: cell.formattedValue,
url: "",
isUrlEditable: true,
};
}
get menuPosition(): DOMCoordinates {
return {
x: this.position.x + MENU_OFFSET_X - PADDING - 2,
y: this.position.y + MENU_OFFSET_Y,
};
}
onSpecialLink(ev: CustomEvent<string>) {
const { detail: markdownLink } = ev;
const link = detectLink(markdownLink);
if (!link) {
return;
}
this.link.url = link.url;
this.link.label = link.label;
this.link.isUrlEditable = link.isUrlEditable;
}
getUrlRepresentation(link: Link): string {
return urlRepresentation(link, this.env.model.getters);
}
openMenu() {
this.menu.isOpen = true;
}
removeLink() {
this.link.url = "";
this.link.isUrlEditable = true;
}
save() {
const { col, row } = this.props.cellPosition;
const locale = this.env.model.getters.getLocale();
const label = this.link.label
? canonicalizeNumberContent(this.link.label, locale)
: this.link.url;
this.env.model.dispatch("UPDATE_CELL", {
col: col,
row: row,
sheetId: this.env.model.getters.getActiveSheetId(),
content: markdownLink(label, this.link.url),
});
this.props.onClosed?.();
}
cancel() {
this.props.onClosed?.();
}
onKeyDown(ev: KeyboardEvent) {
switch (ev.key) {
case "Enter":
if (this.link.url) {
this.save();
}
ev.stopPropagation();
ev.preventDefault();
break;
case "Escape":
this.cancel();
ev.stopPropagation();
break;
}
}
}
export const LinkEditorPopoverBuilder: PopoverBuilders = {
onOpen: (position, getters): CellPopoverComponent<typeof LinkEditor> => {
return {
isOpen: true,
props: { cellPosition: position },
Component: LinkEditor,
cellCorner: "BottomLeft",
};
},
};