Skip to content

Show content-link in toolbar in image caption field #2992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/components/SlateEditor/plugins/image/ImageEmbedForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ const InputWrapper = styled("div", {
},
});

const toolbarOptions = {
inline: {
"content-link": { hidden: false },
},
};

const EmbedForm = ({
onClose,
language,
Expand Down Expand Up @@ -180,6 +186,7 @@ const EmbedForm = ({
</ContentEditableFieldLabel>
<InlineField
{...field}
toolbarOptions={toolbarOptions}
placeholder={t("form.image.caption.placeholder")}
submitted={isSubmitting}
onChange={helpers.setValue}
Expand Down
11 changes: 9 additions & 2 deletions src/components/SlateEditor/plugins/link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ export interface Model {

const Link = ({ attributes, editor, element, children }: Props) => {
const linkRef = useRef<HTMLAnchorElement>(null);
const editorWrapperRef = useRef<HTMLElement>(null);
const [editMode, setEditMode] = useState(false);
const language = useArticleLanguage();
const { t } = useTranslation();

useEffect(() => {
if (linkRef.current) {
editorWrapperRef.current = linkRef.current.closest("[data-slate-wrapper]");
}
}, []);

useEffect(() => {
setEditMode(!!element.isFirstEdit);
}, [element]);
Expand Down Expand Up @@ -161,7 +168,7 @@ const Link = ({ attributes, editor, element, children }: Props) => {
<InlineBugfix />
</a>
</PopoverTrigger>
<Portal>
<Portal container={{ current: editorWrapperRef.current }}>
<StyledPopoverContent>
<DialogTrigger asChild>
<Button variant="link">{t("form.content.link.change")}</Button>
Expand All @@ -172,7 +179,7 @@ const Link = ({ attributes, editor, element, children }: Props) => {
</StyledPopoverContent>
</Portal>
</PopoverRoot>
<Portal>
<Portal container={{ current: editorWrapperRef.current }}>
<DialogContent>
<DialogHeader>
<DialogTitle>{t(`form.content.link.${element.data ? "changeTitle" : "addTitle"}`)}</DialogTitle>
Expand Down
2 changes: 2 additions & 0 deletions src/components/SlateEditor/plugins/noop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const noopPlugin = _noopPlugin.configure({
inlineBlocks: inlineElements,
},
});

export const inlineNoopPlugin = _noopPlugin.configure({ options: { inlineBlocks: { value: [], override: true } } });
89 changes: 56 additions & 33 deletions src/containers/FormikForm/InlineField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
*
*/

import { merge } from "lodash-es";
import { useMemo } from "react";
import { Text, TextArea } from "@ndla/primitives";
import { styled } from "@ndla/styled-system/jsx";
import { SlatePlugin } from "../../components/SlateEditor/interfaces";
import { breakPlugin } from "../../components/SlateEditor/plugins/break";
import { breakRenderer } from "../../components/SlateEditor/plugins/break/render";
import { contentLinkPlugin, linkPlugin } from "../../components/SlateEditor/plugins/link";
import { linkRenderer } from "../../components/SlateEditor/plugins/link/render";
import { markPlugin } from "../../components/SlateEditor/plugins/mark";
import { markRenderer } from "../../components/SlateEditor/plugins/mark/render";
import { noopPlugin } from "../../components/SlateEditor/plugins/noop";
import { inlineNoopPlugin } from "../../components/SlateEditor/plugins/noop";
import { noopRenderer } from "../../components/SlateEditor/plugins/noop/render";
import { paragraphPlugin } from "../../components/SlateEditor/plugins/paragraph";
import { paragraphRenderer } from "../../components/SlateEditor/plugins/paragraph/render";
Expand All @@ -23,6 +27,8 @@ import { spanRenderer } from "../../components/SlateEditor/plugins/span/render";
import { textTransformPlugin } from "../../components/SlateEditor/plugins/textTransform";
import { toolbarPlugin } from "../../components/SlateEditor/plugins/toolbar";
import {
AreaFilters,
CategoryFilters,
createToolbarAreaOptions,
createToolbarDefaultValues,
} from "../../components/SlateEditor/plugins/toolbar/toolbarState";
Expand All @@ -31,22 +37,14 @@ import { unsupportedElementRenderer } from "../../components/SlateEditor/plugins
import { unsupportedPlugin } from "../../components/SlateEditor/plugins/unsupported/unsupportedPlugin";
import RichTextEditor, { RichTextEditorProps } from "../../components/SlateEditor/RichTextEditor";

interface Props extends Omit<RichTextEditorProps, "toolbarOptions" | "toolbarAreaFilters"> {}

const toolbarOptions = createToolbarDefaultValues({
text: {
hidden: true,
},
mark: {
code: {
hidden: true,
},
},
block: { hidden: true },
inline: {
hidden: true,
},
});
interface Props
extends Omit<
RichTextEditorProps,
"hideBlockPicker" | "plugins" | "toolbarOptions" | "toolbarAreaFilters" | "renderPlaceholder"
> {
toolbarOptions?: CategoryFilters;
toolbarAreaFilters?: AreaFilters;
}

const StyledText = styled(Text, {
base: {
Expand All @@ -55,39 +53,64 @@ const StyledText = styled(Text, {
},
});

const toolbarAreaFilters = createToolbarAreaOptions();

const inlinePlugins: SlatePlugin[] = [
spanPlugin,
paragraphPlugin,
toolbarPlugin(toolbarOptions, toolbarAreaFilters),
textTransformPlugin,
breakPlugin,
saveHotkeyPlugin,
markPlugin,
noopPlugin,
unsupportedPlugin,
];

const StyledTextArea = styled(TextArea, {
base: {
minHeight: "unset",
height: "unset",
},
});

const defaultToolbarOptions = createToolbarDefaultValues({
text: {
hidden: true,
},
mark: {
code: {
hidden: true,
},
},
block: { hidden: true },
inline: {
hidden: true,
},
});

const renderers: SlatePlugin[] = [
noopRenderer,
paragraphRenderer,
markRenderer,
breakRenderer,
spanRenderer,
linkRenderer,
unsupportedElementRenderer,
];

const plugins = inlinePlugins.concat(renderers);
export const InlineField = ({
toolbarOptions: toolbarOptionsProp,
toolbarAreaFilters: toolbarAreaFiltersProp,
...rest
}: Props) => {
const { toolbarOptions, toolbarAreaFilters, plugins } = useMemo(() => {
const toolbarOptions = merge({}, defaultToolbarOptions, toolbarOptionsProp);
const toolbarAreaFilters = createToolbarAreaOptions(toolbarAreaFiltersProp);

const inlinePlugins: SlatePlugin[] = [
spanPlugin,
paragraphPlugin,
toolbarPlugin(toolbarOptions, toolbarAreaFilters),
textTransformPlugin,
breakPlugin,
saveHotkeyPlugin,
markPlugin,
inlineNoopPlugin,
linkPlugin,
contentLinkPlugin,
unsupportedPlugin,
];

return { toolbarOptions, toolbarAreaFilters, plugins: inlinePlugins.concat(renderers) };
}, [toolbarOptionsProp, toolbarAreaFiltersProp]);

export const InlineField = ({ ...rest }: Props) => {
return (
<StyledTextArea asChild>
<RichTextEditor
Expand Down