-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Graham Langford <[email protected]> Co-authored-by: Graham Langford <[email protected]>
- Loading branch information
1 parent
f427855
commit 0571b08
Showing
16 changed files
with
645 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...browser-extension/src/components/richTextEditor/toolbar/LinkButton/LinkButton.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*! | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
@import "@/themes/colors"; | ||
|
||
.linkPopover { | ||
font-size: 1rem; | ||
padding: 4px 8px; | ||
max-width: none; | ||
|
||
:global(.form-label) { | ||
margin: 0 8px 0 0; | ||
text-wrap: nowrap; | ||
} | ||
|
||
:global(.form-control) { | ||
height: 26px; | ||
width: 150px; | ||
margin: 0 8px 0 0; | ||
} | ||
|
||
:global(.btn) { | ||
padding: 0; | ||
} | ||
|
||
span { | ||
color: $N800; | ||
} | ||
|
||
label { | ||
color: $N800; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...tions/browser-extension/src/components/richTextEditor/toolbar/LinkButton/LinkEditForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React, { type Dispatch, type SetStateAction, useCallback } from "react"; | ||
import { | ||
type PopoverState, | ||
POPOVER_VIEW, | ||
} from "@/components/richTextEditor/toolbar/LinkButton/types"; | ||
import { assertNotNullish } from "@/utils/nullishUtils"; | ||
import { useCurrentEditor } from "@tiptap/react"; | ||
// eslint-disable-next-line no-restricted-imports -- Not a schema-driven form | ||
import { Formik } from "formik"; | ||
// eslint-disable-next-line no-restricted-imports -- Not a schema-driven form | ||
import { Form, Button } from "react-bootstrap"; | ||
|
||
const LinkEditForm: React.FC<{ | ||
initialHref: string; | ||
setPopoverState: Dispatch<SetStateAction<PopoverState>>; | ||
}> = ({ initialHref, setPopoverState }) => { | ||
const { editor } = useCurrentEditor(); | ||
assertNotNullish(editor, "Tiptap editor must be in scope"); | ||
|
||
const onSubmit = useCallback( | ||
(url: string) => { | ||
if (!url || url === "") { | ||
editor.chain().focus().extendMarkRange("link").unsetLink().run(); | ||
} else { | ||
editor | ||
.chain() | ||
.focus() | ||
.extendMarkRange("link") | ||
.setLink({ href: url }) | ||
.run(); | ||
} | ||
|
||
setPopoverState({ | ||
showPopover: false, | ||
popoverView: POPOVER_VIEW.editForm, | ||
}); | ||
}, | ||
[editor, setPopoverState], | ||
); | ||
|
||
return ( | ||
<Formik | ||
initialValues={{ newUrl: initialHref }} | ||
onSubmit={(values) => { | ||
onSubmit(values.newUrl); | ||
}} | ||
> | ||
{({ handleSubmit, handleChange, handleBlur, values }) => ( | ||
<Form inline onSubmit={handleSubmit}> | ||
<Form.Label htmlFor="newUrl">Enter link:</Form.Label> | ||
<Form.Control | ||
id="newUrl" | ||
name="newUrl" | ||
size="sm" | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
value={values.newUrl} | ||
/> | ||
<Button variant="link" type="submit" size="sm"> | ||
Submit | ||
</Button> | ||
</Form> | ||
)} | ||
</Formik> | ||
); | ||
}; | ||
|
||
export default LinkEditForm; |
44 changes: 44 additions & 0 deletions
44
...browser-extension/src/components/richTextEditor/toolbar/LinkButton/LinkPreviewActions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { truncate } from "lodash"; | ||
import React from "react"; | ||
import { Button } from "react-bootstrap"; | ||
|
||
const LinkPreviewActions: React.FC<{ | ||
href: string; | ||
onEdit: () => void; | ||
onRemove: () => void; | ||
}> = ({ href, onEdit, onRemove }) => ( | ||
<span className="d-flex align-items-center"> | ||
<span className="text-nowrap mr-1">Visit url:</span> | ||
<a href={href} target="_blank" rel="noopener noreferrer" className="mr-2"> | ||
{truncate(href, { | ||
length: 20, | ||
omission: "...", | ||
})} | ||
</a> | ||
<Button variant="link" onClick={onEdit} className="mr-2"> | ||
Edit | ||
</Button> | ||
<Button variant="link" onClick={onRemove}> | ||
Remove | ||
</Button> | ||
</span> | ||
); | ||
|
||
export default LinkPreviewActions; |
75 changes: 75 additions & 0 deletions
75
...ns/browser-extension/src/components/richTextEditor/toolbar/LinkButton/UrlInputPopover.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React, { type Dispatch, type SetStateAction } from "react"; | ||
import LinkEditForm from "@/components/richTextEditor/toolbar/LinkButton/LinkEditForm"; | ||
import LinkPreviewActions from "@/components/richTextEditor/toolbar/LinkButton/LinkPreviewActions"; | ||
import { | ||
type PopoverState, | ||
POPOVER_VIEW, | ||
} from "@/components/richTextEditor/toolbar/LinkButton/types"; | ||
import { assertNotNullish } from "@/utils/nullishUtils"; | ||
import { useCurrentEditor } from "@tiptap/react"; | ||
|
||
const UrlInputPopover = ({ | ||
setPopoverState, | ||
popoverView, | ||
}: { | ||
setPopoverState: Dispatch<SetStateAction<PopoverState>>; | ||
popoverView: PopoverState["popoverView"]; | ||
}) => { | ||
const { editor } = useCurrentEditor(); | ||
assertNotNullish(editor, "Tiptap editor must be in scope"); | ||
|
||
switch (popoverView) { | ||
case POPOVER_VIEW.linkPreview: { | ||
return ( | ||
<LinkPreviewActions | ||
href={editor.getAttributes("link").href as string} | ||
onEdit={() => { | ||
setPopoverState({ | ||
showPopover: true, | ||
popoverView: POPOVER_VIEW.editForm, | ||
}); | ||
}} | ||
onRemove={() => { | ||
editor.chain().focus().extendMarkRange("link").unsetLink().run(); | ||
setPopoverState({ | ||
showPopover: false, | ||
popoverView: POPOVER_VIEW.linkPreview, | ||
}); | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
case POPOVER_VIEW.editForm: { | ||
return ( | ||
<LinkEditForm | ||
initialHref={(editor.getAttributes("link").href as string) ?? ""} | ||
setPopoverState={setPopoverState} | ||
/> | ||
); | ||
} | ||
|
||
default: { | ||
return null; | ||
} | ||
} | ||
}; | ||
|
||
export default UrlInputPopover; |
Oops, something went wrong.