mirror of
https://github.com/docmost/docmost.git
synced 2026-05-09 07:43:06 +08:00
Compare commits
6 Commits
transclusion
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d8b470495 | |||
| c66c08fa78 | |||
| 6046d04375 | |||
| 5d8c11e741 | |||
| de60aa7e61 | |||
| c9fa6e20b3 |
@@ -42,6 +42,11 @@ function pickInitialsColor(name: string) {
|
|||||||
return SAFE_INITIALS_COLORS[hashName(name) % SAFE_INITIALS_COLORS.length];
|
return SAFE_INITIALS_COLORS[hashName(name) % SAFE_INITIALS_COLORS.length];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sanitizeInitialsSource(name: string) {
|
||||||
|
const sanitized = name.replace(/[^\p{L}\p{N}\s]/gu, " ").trim();
|
||||||
|
return sanitized || name;
|
||||||
|
}
|
||||||
|
|
||||||
export const CustomAvatar = React.forwardRef<
|
export const CustomAvatar = React.forwardRef<
|
||||||
HTMLInputElement,
|
HTMLInputElement,
|
||||||
CustomAvatarProps
|
CustomAvatarProps
|
||||||
@@ -49,12 +54,13 @@ export const CustomAvatar = React.forwardRef<
|
|||||||
const avatarLink = getAvatarUrl(avatarUrl, type);
|
const avatarLink = getAvatarUrl(avatarUrl, type);
|
||||||
const resolvedColor =
|
const resolvedColor =
|
||||||
!color || color === "initials" ? pickInitialsColor(name ?? "") : color;
|
!color || color === "initials" ? pickInitialsColor(name ?? "") : color;
|
||||||
|
const initialsSource = sanitizeInitialsSource(name ?? "");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Avatar
|
<Avatar
|
||||||
ref={ref}
|
ref={ref}
|
||||||
src={avatarLink}
|
src={avatarLink}
|
||||||
name={name}
|
name={initialsSource}
|
||||||
alt={name}
|
alt={name}
|
||||||
color={resolvedColor}
|
color={resolvedColor}
|
||||||
{...props}
|
{...props}
|
||||||
|
|||||||
@@ -1,30 +1,26 @@
|
|||||||
import { CommandProps, EmojiMenuItemType } from "./types";
|
import { CommandProps, EmojiMenuItemType } from "./types";
|
||||||
import { SearchIndex } from "emoji-mart";
|
import { buildEmojiIndex, getFrequentlyUsedEmoji, sortFrequentlyUsedEmoji } from "./utils";
|
||||||
import { getFrequentlyUsedEmoji, sortFrequentlyUsedEmoji } from "./utils";
|
|
||||||
|
|
||||||
const searchEmoji = async (value: string): Promise<EmojiMenuItemType[]> => {
|
const MAX_RESULTS = 5;
|
||||||
if (value === "") {
|
|
||||||
const frequentlyUsedEmoji = getFrequentlyUsedEmoji();
|
const searchEmoji = async (query: string): Promise<EmojiMenuItemType[]> => {
|
||||||
return sortFrequentlyUsedEmoji(frequentlyUsedEmoji);
|
if (query === "") {
|
||||||
|
return sortFrequentlyUsedEmoji(getFrequentlyUsedEmoji());
|
||||||
}
|
}
|
||||||
|
|
||||||
const emojis = await SearchIndex.search(value);
|
const q = query.toLowerCase();
|
||||||
const results = emojis.map((emoji: any) => {
|
const index = await buildEmojiIndex();
|
||||||
return {
|
|
||||||
id: emoji.id,
|
|
||||||
emoji: emoji.skins[0].native,
|
|
||||||
command: ({ editor, range }: CommandProps) => {
|
|
||||||
editor
|
|
||||||
.chain()
|
|
||||||
.focus()
|
|
||||||
.deleteRange(range)
|
|
||||||
.insertContent(emoji.skins[0].native + " ")
|
|
||||||
.run();
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return results;
|
return index
|
||||||
|
.filter((e) => e.name.includes(q) || e.id.includes(q))
|
||||||
|
.slice(0, MAX_RESULTS)
|
||||||
|
.map((entry) => ({
|
||||||
|
id: entry.id,
|
||||||
|
emoji: entry.native,
|
||||||
|
command: ({ editor, range }: CommandProps) => {
|
||||||
|
editor.chain().focus().deleteRange(range).insertContent(entry.native + " ").run();
|
||||||
|
},
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getEmojiItems = async ({
|
export const getEmojiItems = async ({
|
||||||
|
|||||||
@@ -1,154 +1,208 @@
|
|||||||
import {
|
import { Loader, Paper, ScrollArea, Text, UnstyledButton } from "@mantine/core";
|
||||||
ActionIcon,
|
|
||||||
Loader,
|
|
||||||
Paper,
|
|
||||||
ScrollArea,
|
|
||||||
SimpleGrid,
|
|
||||||
Text,
|
|
||||||
} from "@mantine/core";
|
|
||||||
import { EmojiMenuItemType } from "./types";
|
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
import classes from "./emoji-menu.module.css";
|
|
||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { GRID_COLUMNS, incrementEmojiUsage } from "./utils";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { EmojiMenuItemType } from "./types";
|
||||||
|
import {
|
||||||
|
EmojiCategory,
|
||||||
|
EmojiIndexEntry,
|
||||||
|
getEmojiCategories,
|
||||||
|
incrementEmojiUsage,
|
||||||
|
} from "./utils";
|
||||||
|
import classes from "./emoji-menu.module.css";
|
||||||
|
|
||||||
const EmojiList = ({
|
const COLS = 8;
|
||||||
|
|
||||||
|
const CAT_ICONS: Record<string, string> = {
|
||||||
|
people: "😀",
|
||||||
|
nature: "🌿",
|
||||||
|
foods: "🍕",
|
||||||
|
activity: "🎮",
|
||||||
|
places: "🗺️",
|
||||||
|
objects: "🔧",
|
||||||
|
symbols: "💯",
|
||||||
|
flags: "🚩",
|
||||||
|
};
|
||||||
|
|
||||||
|
function EmojiList({
|
||||||
items,
|
items,
|
||||||
isLoading,
|
isLoading,
|
||||||
command,
|
command,
|
||||||
editor,
|
editor,
|
||||||
range,
|
range,
|
||||||
|
query = "",
|
||||||
}: {
|
}: {
|
||||||
items: EmojiMenuItemType[];
|
items: EmojiMenuItemType[];
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
command: any;
|
command: (item: EmojiMenuItemType) => void;
|
||||||
editor: any;
|
editor: any;
|
||||||
range: any;
|
range: any;
|
||||||
}) => {
|
query?: string;
|
||||||
const [selectedIndex, setSelectedIndex] = useState(0);
|
}) {
|
||||||
const viewportRef = useRef<HTMLDivElement>(null);
|
const { t } = useTranslation();
|
||||||
|
const [idx, setIdx] = useState(0);
|
||||||
|
const [cats, setCats] = useState<EmojiCategory[]>([]);
|
||||||
|
const [activeCat, setActiveCat] = useState("");
|
||||||
|
const [focusZone, setFocusZone] = useState<"grid" | "tabs">("grid");
|
||||||
|
const listViewport = useRef<HTMLDivElement>(null);
|
||||||
|
const gridViewport = useRef<HTMLDivElement>(null);
|
||||||
|
const catBar = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const selectItem = useCallback(
|
const searching = query.length > 0;
|
||||||
(index: number) => {
|
const browseLoading = !searching && cats.length === 0;
|
||||||
const item = items[index];
|
const gridItems = cats.find((c) => c.id === activeCat)?.emojis ?? [];
|
||||||
if (item) {
|
|
||||||
command(item);
|
useEffect(() => {
|
||||||
incrementEmojiUsage(item.id);
|
getEmojiCategories().then((data) => {
|
||||||
}
|
setCats(data);
|
||||||
|
setActiveCat((prev) => prev || data[0]?.id || "");
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => { setIdx(0); }, [query, activeCat]);
|
||||||
|
|
||||||
|
useEffect(() => { if (searching) setFocusZone("grid"); }, [searching]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (focusZone !== "tabs") return;
|
||||||
|
catBar.current?.querySelector<HTMLElement>(`[data-cat="${activeCat}"]`)?.scrollIntoView({ block: "nearest", inline: "nearest" });
|
||||||
|
}, [activeCat, focusZone]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (focusZone === "tabs") return;
|
||||||
|
const vp = searching ? listViewport.current : gridViewport.current;
|
||||||
|
vp?.querySelector<HTMLElement>(`[data-i="${idx}"]`)?.scrollIntoView({ block: "nearest" });
|
||||||
|
}, [idx, searching, focusZone]);
|
||||||
|
|
||||||
|
const pickSearchItem = useCallback(
|
||||||
|
(i: number) => {
|
||||||
|
const item = items[i];
|
||||||
|
if (!item) return;
|
||||||
|
command(item);
|
||||||
|
incrementEmojiUsage(item.id);
|
||||||
},
|
},
|
||||||
[command, items]
|
[command, items],
|
||||||
|
);
|
||||||
|
|
||||||
|
const pickGridItem = useCallback(
|
||||||
|
(entry: EmojiIndexEntry) => {
|
||||||
|
editor.chain().focus().deleteRange(range).insertContent(entry.native + " ").run();
|
||||||
|
incrementEmojiUsage(entry.id);
|
||||||
|
},
|
||||||
|
[editor, range],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const navigationKeys = [
|
function onKey(e: KeyboardEvent) {
|
||||||
"ArrowRight",
|
if (searching) {
|
||||||
"ArrowLeft",
|
if (e.key === "ArrowDown") { e.preventDefault(); setIdx((i) => Math.min(i + 1, items.length - 1)); }
|
||||||
"ArrowUp",
|
else if (e.key === "ArrowUp") { e.preventDefault(); setIdx((i) => Math.max(i - 1, 0)); }
|
||||||
"ArrowDown",
|
else if (e.key === "Enter") { e.preventDefault(); pickSearchItem(idx); }
|
||||||
"Enter",
|
} else if (focusZone === "tabs") {
|
||||||
];
|
const catIdx = cats.findIndex((c) => c.id === activeCat);
|
||||||
const onKeyDown = (e: KeyboardEvent) => {
|
if (e.key === "ArrowRight") { e.preventDefault(); const next = cats[Math.min(catIdx + 1, cats.length - 1)]; if (next) setActiveCat(next.id); }
|
||||||
if (navigationKeys.includes(e.key)) {
|
else if (e.key === "ArrowLeft") { e.preventDefault(); const prev = cats[Math.max(catIdx - 1, 0)]; if (prev) setActiveCat(prev.id); }
|
||||||
e.preventDefault();
|
else if (e.key === "ArrowDown" || e.key === "Enter") { e.preventDefault(); setFocusZone("grid"); }
|
||||||
|
else if (e.key === "ArrowUp") { e.preventDefault(); }
|
||||||
if (e.key === "ArrowRight") {
|
} else {
|
||||||
setSelectedIndex(
|
const total = gridItems.length;
|
||||||
selectedIndex + 1 < items.length ? selectedIndex + 1 : selectedIndex
|
if (e.key === "ArrowRight") { e.preventDefault(); setIdx((i) => Math.min(i + 1, total - 1)); }
|
||||||
);
|
else if (e.key === "ArrowLeft") { e.preventDefault(); setIdx((i) => Math.max(i - 1, 0)); }
|
||||||
return true;
|
else if (e.key === "ArrowDown") { e.preventDefault(); setIdx((i) => Math.min(i + COLS, total - 1)); }
|
||||||
|
else if (e.key === "ArrowUp") {
|
||||||
|
e.preventDefault();
|
||||||
|
if (idx < COLS) setFocusZone("tabs");
|
||||||
|
else setIdx((i) => Math.max(i - COLS, 0));
|
||||||
}
|
}
|
||||||
|
else if (e.key === "Enter") { e.preventDefault(); if (gridItems[idx]) pickGridItem(gridItems[idx]); }
|
||||||
if (e.key === "ArrowLeft") {
|
|
||||||
setSelectedIndex(
|
|
||||||
selectedIndex - 1 >= 0 ? selectedIndex - 1 : selectedIndex
|
|
||||||
);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.key === "ArrowUp") {
|
|
||||||
setSelectedIndex(
|
|
||||||
selectedIndex - GRID_COLUMNS >= 0
|
|
||||||
? selectedIndex - GRID_COLUMNS
|
|
||||||
: selectedIndex
|
|
||||||
);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.key === "ArrowDown") {
|
|
||||||
setSelectedIndex(
|
|
||||||
selectedIndex + GRID_COLUMNS < items.length
|
|
||||||
? selectedIndex + GRID_COLUMNS
|
|
||||||
: selectedIndex
|
|
||||||
);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.key === "Enter") {
|
|
||||||
selectItem(selectedIndex);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
document.addEventListener("keydown", onKeyDown);
|
document.addEventListener("keydown", onKey);
|
||||||
return () => {
|
return () => document.removeEventListener("keydown", onKey);
|
||||||
document.removeEventListener("keydown", onKeyDown);
|
}, [searching, items, idx, gridItems, pickSearchItem, pickGridItem, focusZone, cats, activeCat]);
|
||||||
};
|
|
||||||
}, [items, selectedIndex, setSelectedIndex]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
return (
|
||||||
setSelectedIndex(0);
|
|
||||||
}, [items]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
viewportRef.current
|
|
||||||
?.querySelector(`[data-item-index="${selectedIndex}"]`)
|
|
||||||
?.scrollIntoView({ block: "nearest" });
|
|
||||||
}, [selectedIndex]);
|
|
||||||
|
|
||||||
return items.length > 0 || isLoading ? (
|
|
||||||
<Paper
|
<Paper
|
||||||
id="emoji-command"
|
id="emoji-command"
|
||||||
p="0"
|
p={0}
|
||||||
shadow="md"
|
shadow="md"
|
||||||
withBorder
|
withBorder
|
||||||
|
style={{ width: 280 }}
|
||||||
role="listbox"
|
role="listbox"
|
||||||
aria-label="Emoji results"
|
aria-label={t("Emoji picker")}
|
||||||
aria-activedescendant={
|
|
||||||
items.length > 0 ? `emoji-command-option-${selectedIndex}` : undefined
|
|
||||||
}
|
|
||||||
>
|
>
|
||||||
{isLoading && <Loader m="xs" color="blue" type="dots" />}
|
{searching ? (
|
||||||
{items.length > 0 && (
|
<>
|
||||||
<ScrollArea.Autosize
|
{isLoading && <Loader m="xs" size="xs" color="blue" type="dots" />}
|
||||||
viewportRef={viewportRef}
|
<ScrollArea.Autosize mah={260} scrollbarSize={6} viewportRef={listViewport}>
|
||||||
mah={250}
|
<div style={{ padding: 4 }}>
|
||||||
scrollbarSize={8}
|
{items.length === 0 && !isLoading ? (
|
||||||
pr="5"
|
<Text size="sm" c="dimmed" p="xs">{t("No results")}</Text>
|
||||||
>
|
) : items.map((item, i) => (
|
||||||
<SimpleGrid cols={GRID_COLUMNS} p="xs" spacing="xs">
|
<UnstyledButton
|
||||||
{items.map((item, index: number) => (
|
key={item.id}
|
||||||
<ActionIcon
|
data-i={i}
|
||||||
data-item-index={index}
|
w="100%"
|
||||||
id={`emoji-command-option-${index}`}
|
className={clsx(classes.row, { [classes.active]: i === idx })}
|
||||||
role="option"
|
onClick={() => pickSearchItem(i)}
|
||||||
aria-selected={index === selectedIndex}
|
onMouseEnter={() => setIdx(i)}
|
||||||
aria-label={item.id}
|
role="option"
|
||||||
variant="transparent"
|
aria-selected={i === idx}
|
||||||
key={item.id}
|
>
|
||||||
className={clsx(classes.menuBtn, {
|
<span style={{ fontSize: 20, lineHeight: 1, minWidth: 26 }}>{item.emoji}</span>
|
||||||
[classes.selectedItem]: index === selectedIndex,
|
<Text size="sm" c="dimmed" ff="monospace" span>:{item.id}:</Text>
|
||||||
})}
|
</UnstyledButton>
|
||||||
onClick={() => selectItem(index)}
|
))}
|
||||||
>
|
</div>
|
||||||
<Text size="xl">{item.emoji}</Text>
|
</ScrollArea.Autosize>
|
||||||
</ActionIcon>
|
</>
|
||||||
))}
|
) : browseLoading ? (
|
||||||
</SimpleGrid>
|
<Loader m="xs" size="xs" color="blue" type="dots" />
|
||||||
</ScrollArea.Autosize>
|
) : (
|
||||||
|
<>
|
||||||
|
<div className={classes.catBar} role="tablist" ref={catBar}>
|
||||||
|
{cats.map((c) => {
|
||||||
|
const isActive = c.id === activeCat;
|
||||||
|
const isFocused = isActive && focusZone === "tabs";
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={c.id}
|
||||||
|
data-cat={c.id}
|
||||||
|
title={c.id}
|
||||||
|
role="tab"
|
||||||
|
aria-selected={isActive}
|
||||||
|
className={clsx(classes.catTab, {
|
||||||
|
[classes.catTabActive]: isActive,
|
||||||
|
[classes.catTabFocused]: isFocused,
|
||||||
|
})}
|
||||||
|
onClick={() => { setActiveCat(c.id); setFocusZone("grid"); }}
|
||||||
|
onMouseEnter={() => setFocusZone("grid")}
|
||||||
|
>
|
||||||
|
{CAT_ICONS[c.id] ?? "🔣"}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
<ScrollArea.Autosize mah={220} scrollbarSize={6} viewportRef={gridViewport}>
|
||||||
|
<div className={classes.grid} style={{ gridTemplateColumns: `repeat(${COLS}, 1fr)` }}>
|
||||||
|
{gridItems.map((entry, i) => (
|
||||||
|
<button
|
||||||
|
key={entry.id}
|
||||||
|
data-i={i}
|
||||||
|
title={`:${entry.id}:`}
|
||||||
|
className={clsx(classes.emojiBtn, { [classes.active]: i === idx })}
|
||||||
|
onClick={() => pickGridItem(entry)}
|
||||||
|
onMouseEnter={() => setIdx(i)}
|
||||||
|
>
|
||||||
|
{entry.native}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</ScrollArea.Autosize>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</Paper>
|
</Paper>
|
||||||
) : null;
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
export default EmojiList;
|
export default EmojiList;
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
.menuBtn {
|
.row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 6px 10px;
|
||||||
border-radius: var(--mantine-radius-sm);
|
border-radius: var(--mantine-radius-sm);
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
@mixin light {
|
@mixin light {
|
||||||
background: var(--mantine-color-gray-2);
|
background: var(--mantine-color-gray-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@mixin dark {
|
@mixin dark {
|
||||||
@@ -12,7 +16,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.selectedItem {
|
.active {
|
||||||
@mixin light {
|
@mixin light {
|
||||||
background: var(--mantine-color-gray-2);
|
background: var(--mantine-color-gray-2);
|
||||||
}
|
}
|
||||||
@@ -21,3 +25,83 @@
|
|||||||
background: var(--mantine-color-gray-light);
|
background: var(--mantine-color-gray-light);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.catBar {
|
||||||
|
display: flex;
|
||||||
|
gap: 2px;
|
||||||
|
padding: 4px 6px;
|
||||||
|
overflow-x: auto;
|
||||||
|
scrollbar-width: none;
|
||||||
|
|
||||||
|
@mixin light {
|
||||||
|
border-bottom: 1px solid var(--mantine-color-gray-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@mixin dark {
|
||||||
|
border-bottom: 1px solid var(--mantine-color-dark-4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.catTab {
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1;
|
||||||
|
padding: 4px 5px;
|
||||||
|
border-radius: var(--mantine-radius-sm);
|
||||||
|
flex-shrink: 0;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
@mixin light {
|
||||||
|
background: var(--mantine-color-gray-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@mixin dark {
|
||||||
|
background: var(--mantine-color-gray-light);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.catTabActive {
|
||||||
|
@mixin light {
|
||||||
|
background: var(--mantine-color-gray-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@mixin dark {
|
||||||
|
background: var(--mantine-color-gray-light);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.catTabFocused {
|
||||||
|
outline: 1px solid var(--mantine-color-blue-filled);
|
||||||
|
outline-offset: -1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.grid {
|
||||||
|
display: grid;
|
||||||
|
gap: 1px;
|
||||||
|
padding: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.emojiBtn {
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 20px;
|
||||||
|
line-height: 1;
|
||||||
|
padding: 3px;
|
||||||
|
border-radius: var(--mantine-radius-sm);
|
||||||
|
aspect-ratio: 1 / 1;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
@mixin light {
|
||||||
|
background: var(--mantine-color-gray-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@mixin dark {
|
||||||
|
background: var(--mantine-color-gray-light);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ReactRenderer, useEditor } from "@tiptap/react";
|
import { ReactRenderer, useEditor } from "@tiptap/react";
|
||||||
import EmojiList from "./emoji-list";
|
import EmojiList from "./emoji-list";
|
||||||
import { init } from "emoji-mart";
|
|
||||||
import {
|
import {
|
||||||
autoUpdate,
|
autoUpdate,
|
||||||
computePosition,
|
computePosition,
|
||||||
@@ -37,10 +36,6 @@ const renderEmojiItems = () => {
|
|||||||
editor: ReturnType<typeof useEditor>;
|
editor: ReturnType<typeof useEditor>;
|
||||||
clientRect: () => DOMRect;
|
clientRect: () => DOMRect;
|
||||||
}) => {
|
}) => {
|
||||||
init({
|
|
||||||
data: async () => (await import("@emoji-mart/data")).default,
|
|
||||||
});
|
|
||||||
|
|
||||||
component = new ReactRenderer(EmojiList, {
|
component = new ReactRenderer(EmojiList, {
|
||||||
props: { isLoading: true, items: [] },
|
props: { isLoading: true, items: [] },
|
||||||
editor: props.editor,
|
editor: props.editor,
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
import { CommandProps } from "./types";
|
import { CommandProps, EmojiMartFrequentlyType, EmojiMenuItemType } from "./types";
|
||||||
import { getEmojiDataFromNative } from "emoji-mart";
|
|
||||||
import { EmojiMartFrequentlyType, EmojiMenuItemType } from "./types";
|
|
||||||
|
|
||||||
export const GRID_COLUMNS = 10;
|
|
||||||
|
|
||||||
export const LOCAL_STORAGE_FREQUENT_KEY = "emoji-mart.frequently";
|
export const LOCAL_STORAGE_FREQUENT_KEY = "emoji-mart.frequently";
|
||||||
|
|
||||||
@@ -19,41 +15,76 @@ export const DEFAULT_FREQUENTLY_USED_EMOJI_MART = `{
|
|||||||
"rocket": 1
|
"rocket": 1
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
export type EmojiIndexEntry = { id: string; name: string; native: string };
|
||||||
|
|
||||||
|
let _emojiIndex: EmojiIndexEntry[] | null = null;
|
||||||
|
|
||||||
|
export const buildEmojiIndex = async (): Promise<EmojiIndexEntry[]> => {
|
||||||
|
if (_emojiIndex) return _emojiIndex;
|
||||||
|
const { default: data } = await import("@emoji-mart/data");
|
||||||
|
_emojiIndex = (Object.values((data as any).emojis) as any[])
|
||||||
|
.filter((e) => e.id && e.name && e.skins?.[0]?.native)
|
||||||
|
.map((e) => ({
|
||||||
|
id: e.id as string,
|
||||||
|
name: (e.name as string).toLowerCase(),
|
||||||
|
native: e.skins[0].native as string,
|
||||||
|
}));
|
||||||
|
return _emojiIndex;
|
||||||
|
};
|
||||||
|
|
||||||
export const incrementEmojiUsage = (emojiId: string) => {
|
export const incrementEmojiUsage = (emojiId: string) => {
|
||||||
const frequentlyUsedEmoji =
|
const stored = JSON.parse(
|
||||||
JSON.parse(localStorage.getItem(LOCAL_STORAGE_FREQUENT_KEY) || DEFAULT_FREQUENTLY_USED_EMOJI_MART);
|
localStorage.getItem(LOCAL_STORAGE_FREQUENT_KEY) || DEFAULT_FREQUENTLY_USED_EMOJI_MART,
|
||||||
frequentlyUsedEmoji[emojiId]
|
|
||||||
? (frequentlyUsedEmoji[emojiId] += 1)
|
|
||||||
: (frequentlyUsedEmoji[emojiId] = 1);
|
|
||||||
localStorage.setItem(
|
|
||||||
LOCAL_STORAGE_FREQUENT_KEY,
|
|
||||||
JSON.stringify(frequentlyUsedEmoji)
|
|
||||||
);
|
);
|
||||||
|
stored[emojiId] = (stored[emojiId] ?? 0) + 1;
|
||||||
|
localStorage.setItem(LOCAL_STORAGE_FREQUENT_KEY, JSON.stringify(stored));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const sortFrequentlyUsedEmoji = async (
|
export const sortFrequentlyUsedEmoji = async (
|
||||||
frequentlyUsedEmoji: EmojiMartFrequentlyType
|
frequentlyUsedEmoji: EmojiMartFrequentlyType,
|
||||||
): Promise<EmojiMenuItemType[]> => {
|
): Promise<EmojiMenuItemType[]> => {
|
||||||
const data = await Promise.all(
|
const index = await buildEmojiIndex();
|
||||||
Object.entries(frequentlyUsedEmoji).map(
|
const results: EmojiMenuItemType[] = Object.entries(frequentlyUsedEmoji)
|
||||||
async ([id, count]): Promise<EmojiMenuItemType> => ({
|
.map(([id, count]): EmojiMenuItemType | null => {
|
||||||
|
const entry = index.find((e) => e.id === id);
|
||||||
|
if (!entry) return null;
|
||||||
|
return {
|
||||||
id,
|
id,
|
||||||
count,
|
count,
|
||||||
emoji: (await getEmojiDataFromNative(id))?.native,
|
emoji: entry.native,
|
||||||
command: async ({ editor, range }: CommandProps) => {
|
command: ({ editor, range }: CommandProps) => {
|
||||||
editor
|
editor.chain().focus().deleteRange(range).insertContent(entry.native + " ").run();
|
||||||
.chain()
|
|
||||||
.focus()
|
|
||||||
.deleteRange(range)
|
|
||||||
.insertContent((await getEmojiDataFromNative(id))?.native + " ")
|
|
||||||
.run();
|
|
||||||
},
|
},
|
||||||
})
|
};
|
||||||
)
|
})
|
||||||
);
|
.filter((e): e is EmojiMenuItemType => e !== null);
|
||||||
return data.sort((a, b) => b.count - a.count);
|
return results.sort((a, b) => (b.count ?? 0) - (a.count ?? 0)).slice(0, 5);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getFrequentlyUsedEmoji = () => {
|
export const getFrequentlyUsedEmoji = (): EmojiMartFrequentlyType => {
|
||||||
return JSON.parse(localStorage.getItem(LOCAL_STORAGE_FREQUENT_KEY) || DEFAULT_FREQUENTLY_USED_EMOJI_MART);
|
return JSON.parse(
|
||||||
}
|
localStorage.getItem(LOCAL_STORAGE_FREQUENT_KEY) || DEFAULT_FREQUENTLY_USED_EMOJI_MART,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export type EmojiCategory = { id: string; emojis: EmojiIndexEntry[] };
|
||||||
|
|
||||||
|
let _cats: EmojiCategory[] | null = null;
|
||||||
|
|
||||||
|
export const getEmojiCategories = async (): Promise<EmojiCategory[]> => {
|
||||||
|
if (_cats) return _cats;
|
||||||
|
const [{ default: data }, index] = await Promise.all([
|
||||||
|
import("@emoji-mart/data"),
|
||||||
|
buildEmojiIndex(),
|
||||||
|
]);
|
||||||
|
const byId = new Map(index.map((e) => [e.id, e]));
|
||||||
|
_cats = ((data as any).categories as { id: string; emojis: string[] }[])
|
||||||
|
.map((cat) => ({
|
||||||
|
id: cat.id,
|
||||||
|
emojis: cat.emojis
|
||||||
|
.map((id) => byId.get(id))
|
||||||
|
.filter((e): e is EmojiIndexEntry => !!e),
|
||||||
|
}))
|
||||||
|
.filter((c) => c.emojis.length > 0);
|
||||||
|
return _cats;
|
||||||
|
};
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import {
|
|||||||
IconColumns3,
|
IconColumns3,
|
||||||
IconColumns2,
|
IconColumns2,
|
||||||
IconTag,
|
IconTag,
|
||||||
|
IconMoodSmile,
|
||||||
IconRotate2,
|
IconRotate2,
|
||||||
} from "@tabler/icons-react";
|
} from "@tabler/icons-react";
|
||||||
import {
|
import {
|
||||||
@@ -133,7 +134,7 @@ const CommandGroups: SlashMenuGroupedItemsType = {
|
|||||||
{
|
{
|
||||||
title: "Numbered list",
|
title: "Numbered list",
|
||||||
description: "Create a list with numbering.",
|
description: "Create a list with numbering.",
|
||||||
searchTerms: ["numbered", "ordered", "list"],
|
searchTerms: ["numbered", "ordered", "list", "ol"],
|
||||||
icon: IconListNumbers,
|
icon: IconListNumbers,
|
||||||
command: ({ editor, range }: CommandProps) => {
|
command: ({ editor, range }: CommandProps) => {
|
||||||
editor.chain().focus().deleteRange(range).toggleOrderedList().run();
|
editor.chain().focus().deleteRange(range).toggleOrderedList().run();
|
||||||
@@ -477,10 +478,26 @@ const CommandGroups: SlashMenuGroupedItemsType = {
|
|||||||
.run();
|
.run();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: "Emoji",
|
||||||
|
description: "Insert emoji.",
|
||||||
|
searchTerms: ["emoji", "icon", "smiley", "emoticon", "symbol", "reaction"],
|
||||||
|
icon: IconMoodSmile,
|
||||||
|
command: ({ editor, range }: CommandProps) => {
|
||||||
|
editor.chain().focus().deleteRange(range).insertContent(":").run();
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: "Subpages (Child pages)",
|
title: "Subpages (Child pages)",
|
||||||
description: "List all subpages of the current page",
|
description: "List all subpages of the current page",
|
||||||
searchTerms: ["subpages", "child", "children", "nested", "hierarchy"],
|
searchTerms: [
|
||||||
|
"subpages",
|
||||||
|
"child",
|
||||||
|
"children",
|
||||||
|
"nested",
|
||||||
|
"hierarchy",
|
||||||
|
"toc",
|
||||||
|
],
|
||||||
icon: IconSitemap,
|
icon: IconSitemap,
|
||||||
command: ({ editor, range }: CommandProps) => {
|
command: ({ editor, range }: CommandProps) => {
|
||||||
editor.chain().focus().deleteRange(range).insertSubpages().run();
|
editor.chain().focus().deleteRange(range).insertSubpages().run();
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import { Typography } from "@tiptap/extension-typography";
|
|||||||
import { TextStyle } from "@tiptap/extension-text-style";
|
import { TextStyle } from "@tiptap/extension-text-style";
|
||||||
import { Color } from "@tiptap/extension-color";
|
import { Color } from "@tiptap/extension-color";
|
||||||
import { Youtube } from "@tiptap/extension-youtube";
|
import { Youtube } from "@tiptap/extension-youtube";
|
||||||
import SlashCommand, { SlashCommandExtension as Command } from "@/features/editor/extensions/slash-command";
|
import SlashCommand, {
|
||||||
|
SlashCommandExtension as Command,
|
||||||
|
} from "@/features/editor/extensions/slash-command";
|
||||||
import renderItems from "@/features/editor/components/slash-menu/render-items";
|
import renderItems from "@/features/editor/components/slash-menu/render-items";
|
||||||
import getSuggestionItems from "@/features/editor/components/slash-menu/menu-items";
|
import getSuggestionItems from "@/features/editor/components/slash-menu/menu-items";
|
||||||
import { Collaboration, isChangeOrigin } from "@tiptap/extension-collaboration";
|
import { Collaboration, isChangeOrigin } from "@tiptap/extension-collaboration";
|
||||||
@@ -46,6 +48,7 @@ import {
|
|||||||
Subpages,
|
Subpages,
|
||||||
Heading,
|
Heading,
|
||||||
Highlight,
|
Highlight,
|
||||||
|
Indent,
|
||||||
UniqueID,
|
UniqueID,
|
||||||
SharedStorage,
|
SharedStorage,
|
||||||
Columns,
|
Columns,
|
||||||
@@ -201,6 +204,7 @@ export const mainExtensions = [
|
|||||||
showOnlyWhenEditable: true,
|
showOnlyWhenEditable: true,
|
||||||
}),
|
}),
|
||||||
TextAlign.configure({ types: ["heading", "paragraph"] }),
|
TextAlign.configure({ types: ["heading", "paragraph"] }),
|
||||||
|
Indent,
|
||||||
TaskList,
|
TaskList,
|
||||||
TaskItem.configure({
|
TaskItem.configure({
|
||||||
nested: true,
|
nested: true,
|
||||||
@@ -311,6 +315,8 @@ export const mainExtensions = [
|
|||||||
view: CodeBlockView,
|
view: CodeBlockView,
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
lowlight,
|
lowlight,
|
||||||
|
enableTabIndentation: true,
|
||||||
|
tabSize: 2,
|
||||||
HTMLAttributes: {
|
HTMLAttributes: {
|
||||||
spellcheck: false,
|
spellcheck: false,
|
||||||
},
|
},
|
||||||
@@ -405,7 +411,10 @@ const TEMPLATE_EXCLUDED_SLASH_ITEMS = new Set([
|
|||||||
const TemplateSlashCommand = Command.configure({
|
const TemplateSlashCommand = Command.configure({
|
||||||
suggestion: {
|
suggestion: {
|
||||||
items: ({ query }: { query: string }) =>
|
items: ({ query }: { query: string }) =>
|
||||||
getSuggestionItems({ query, excludeItems: TEMPLATE_EXCLUDED_SLASH_ITEMS }),
|
getSuggestionItems({
|
||||||
|
query,
|
||||||
|
excludeItems: TEMPLATE_EXCLUDED_SLASH_ITEMS,
|
||||||
|
}),
|
||||||
render: renderItems,
|
render: renderItems,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
.ProseMirror {
|
||||||
|
--indent-step: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ProseMirror [data-indent="1"] { padding-inline-start: calc(var(--indent-step) * 1); }
|
||||||
|
.ProseMirror [data-indent="2"] { padding-inline-start: calc(var(--indent-step) * 2); }
|
||||||
|
.ProseMirror [data-indent="3"] { padding-inline-start: calc(var(--indent-step) * 3); }
|
||||||
|
.ProseMirror [data-indent="4"] { padding-inline-start: calc(var(--indent-step) * 4); }
|
||||||
|
.ProseMirror [data-indent="5"] { padding-inline-start: calc(var(--indent-step) * 5); }
|
||||||
|
.ProseMirror [data-indent="6"] { padding-inline-start: calc(var(--indent-step) * 6); }
|
||||||
|
.ProseMirror [data-indent="7"] { padding-inline-start: calc(var(--indent-step) * 7); }
|
||||||
|
.ProseMirror [data-indent="8"] { padding-inline-start: calc(var(--indent-step) * 8); }
|
||||||
|
.ProseMirror [data-indent="9"] { padding-inline-start: calc(var(--indent-step) * 9); }
|
||||||
|
.ProseMirror [data-indent="10"] { padding-inline-start: calc(var(--indent-step) * 10); }
|
||||||
@@ -13,5 +13,6 @@
|
|||||||
@import "./mention.css";
|
@import "./mention.css";
|
||||||
@import "./ordered-list.css";
|
@import "./ordered-list.css";
|
||||||
@import "./highlight.css";
|
@import "./highlight.css";
|
||||||
|
@import "./indent.css";
|
||||||
@import "./columns.css";
|
@import "./columns.css";
|
||||||
@import "./status.css";
|
@import "./status.css";
|
||||||
|
|||||||
@@ -60,6 +60,10 @@ export function UserProvider({ children }: React.PropsWithChildren) {
|
|||||||
}
|
}
|
||||||
}, [data, isLoading]);
|
}, [data, isLoading]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
document.documentElement.lang = i18n.resolvedLanguage || i18n.language || "en-US";
|
||||||
|
}, [i18n.language, i18n.resolvedLanguage]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (entitlements) {
|
if (entitlements) {
|
||||||
setEntitlements(entitlements);
|
setEntitlements(entitlements);
|
||||||
|
|||||||
@@ -163,10 +163,11 @@
|
|||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
"testRegex": ".*\\.spec\\.ts$",
|
"testRegex": ".*\\.spec\\.ts$",
|
||||||
"transform": {
|
"transform": {
|
||||||
|
"happy-dom.+\\.js$": ["babel-jest", { "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]] }],
|
||||||
"^.+\\.(t|j)s$": "ts-jest"
|
"^.+\\.(t|j)s$": "ts-jest"
|
||||||
},
|
},
|
||||||
"transformIgnorePatterns": [
|
"transformIgnorePatterns": [
|
||||||
"/node_modules/(?!(\\.pnpm/)?(nanoid|uuid|image-dimensions|marked)(@|/))"
|
"/node_modules/(?!(\\.pnpm/)?(nanoid|uuid|image-dimensions|marked|happy-dom)(@|/))"
|
||||||
],
|
],
|
||||||
"collectCoverageFrom": [
|
"collectCoverageFrom": [
|
||||||
"**/*.(t|j)s"
|
"**/*.(t|j)s"
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import {
|
|||||||
Mention,
|
Mention,
|
||||||
Subpages,
|
Subpages,
|
||||||
Highlight,
|
Highlight,
|
||||||
|
Indent,
|
||||||
UniqueID,
|
UniqueID,
|
||||||
Columns,
|
Columns,
|
||||||
Column,
|
Column,
|
||||||
@@ -62,10 +63,11 @@ export const tiptapExtensions = [
|
|||||||
}),
|
}),
|
||||||
Heading,
|
Heading,
|
||||||
UniqueID.configure({
|
UniqueID.configure({
|
||||||
types: ['heading', 'paragraph'],
|
types: ['heading', 'paragraph', 'transclusionSource'],
|
||||||
}),
|
}),
|
||||||
Comment,
|
Comment,
|
||||||
TextAlign.configure({ types: ['heading', 'paragraph'] }),
|
TextAlign.configure({ types: ['heading', 'paragraph'] }),
|
||||||
|
Indent,
|
||||||
TaskList,
|
TaskList,
|
||||||
TaskItem.configure({
|
TaskItem.configure({
|
||||||
nested: true,
|
nested: true,
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import { htmlToJson, jsonToHtml } from '../../../collaboration/collaboration.util';
|
||||||
|
|
||||||
|
const findFirstChild = (
|
||||||
|
json: any,
|
||||||
|
type: string,
|
||||||
|
): any | undefined => {
|
||||||
|
if (!json || typeof json !== 'object') return undefined;
|
||||||
|
if (json.type === type) return json;
|
||||||
|
if (Array.isArray(json.content)) {
|
||||||
|
for (const child of json.content) {
|
||||||
|
const found = findFirstChild(child, type);
|
||||||
|
if (found) return found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('indent attribute round-trip', () => {
|
||||||
|
it('parses data-indent on a paragraph into the indent attribute', () => {
|
||||||
|
const html = '<p data-indent="3">Hello</p>';
|
||||||
|
const json = htmlToJson(html);
|
||||||
|
const paragraph = findFirstChild(json, 'paragraph');
|
||||||
|
expect(paragraph).toBeDefined();
|
||||||
|
expect(paragraph.attrs.indent).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses data-indent on a heading into the indent attribute', () => {
|
||||||
|
const html = '<h2 data-indent="2">Heading</h2>';
|
||||||
|
const json = htmlToJson(html);
|
||||||
|
const heading = findFirstChild(json, 'heading');
|
||||||
|
expect(heading).toBeDefined();
|
||||||
|
expect(heading.attrs.indent).toBe(2);
|
||||||
|
expect(heading.attrs.level).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('clamps out-of-range data-indent values', () => {
|
||||||
|
const html = '<p data-indent="42">Too deep</p>';
|
||||||
|
const json = htmlToJson(html);
|
||||||
|
const paragraph = findFirstChild(json, 'paragraph');
|
||||||
|
expect(paragraph.attrs.indent).toBe(8);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders nonzero indent back to data-indent on HTML serialization', () => {
|
||||||
|
const html = '<p data-indent="4">Round-trip</p>';
|
||||||
|
const json = htmlToJson(html);
|
||||||
|
const out = jsonToHtml(json);
|
||||||
|
expect(out).toContain('data-indent="4"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits data-indent for indent zero', () => {
|
||||||
|
const html = '<p>No indent</p>';
|
||||||
|
const json = htmlToJson(html);
|
||||||
|
const out = jsonToHtml(json);
|
||||||
|
expect(out).not.toContain('data-indent');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('preserves indent through HTML → JSON → HTML', () => {
|
||||||
|
const original = '<p data-indent="5">Five deep</p>';
|
||||||
|
const json = htmlToJson(original);
|
||||||
|
const final = jsonToHtml(json);
|
||||||
|
expect(final).toContain('data-indent="5"');
|
||||||
|
});
|
||||||
|
});
|
||||||
+1
-1
Submodule apps/server/src/ee updated: 6479522986...326df8c154
@@ -23,6 +23,7 @@ export * from "./lib/embed-provider";
|
|||||||
export * from "./lib/subpages";
|
export * from "./lib/subpages";
|
||||||
export * from "./lib/transclusion";
|
export * from "./lib/transclusion";
|
||||||
export * from "./lib/highlight";
|
export * from "./lib/highlight";
|
||||||
|
export * from "./lib/indent";
|
||||||
export * from "./lib/heading/heading";
|
export * from "./lib/heading/heading";
|
||||||
export * from "./lib/unique-id";
|
export * from "./lib/unique-id";
|
||||||
export * from "./lib/shared-storage";
|
export * from "./lib/shared-storage";
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import type { CodeBlockOptions } from "@tiptap/extension-code-block";
|
import type { CodeBlockOptions } from '@tiptap/extension-code-block';
|
||||||
import CodeBlock from "@tiptap/extension-code-block";
|
import CodeBlock from '@tiptap/extension-code-block';
|
||||||
|
|
||||||
import { LowlightPlugin } from "./lowlight-plugin.js";
|
import { LowlightPlugin } from './lowlight-plugin.js';
|
||||||
import { ReactNodeViewRenderer } from "@tiptap/react";
|
import { ReactNodeViewRenderer } from '@tiptap/react';
|
||||||
|
|
||||||
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
||||||
/**
|
/**
|
||||||
@@ -12,7 +12,7 @@ export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
|||||||
view: any;
|
view: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TAB_CHAR = "\u00A0\u00A0";
|
const TAB_CHAR = '\u00A0\u00A0';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This extension allows you to highlight code blocks with lowlight.
|
* This extension allows you to highlight code blocks with lowlight.
|
||||||
@@ -25,7 +25,7 @@ export const CustomCodeBlock = CodeBlock.extend<CodeBlockLowlightOptions>({
|
|||||||
return {
|
return {
|
||||||
...this.parent?.(),
|
...this.parent?.(),
|
||||||
lowlight: {},
|
lowlight: {},
|
||||||
languageClassPrefix: "language-",
|
languageClassPrefix: 'language-',
|
||||||
exitOnTripleEnter: true,
|
exitOnTripleEnter: true,
|
||||||
exitOnArrowDown: true,
|
exitOnArrowDown: true,
|
||||||
defaultLanguage: null,
|
defaultLanguage: null,
|
||||||
@@ -37,20 +37,8 @@ export const CustomCodeBlock = CodeBlock.extend<CodeBlockLowlightOptions>({
|
|||||||
addKeyboardShortcuts() {
|
addKeyboardShortcuts() {
|
||||||
return {
|
return {
|
||||||
...this.parent?.(),
|
...this.parent?.(),
|
||||||
Tab: () => {
|
'Mod-a': () => {
|
||||||
if (this.editor.isActive("codeBlock")) {
|
if (this.editor.isActive('codeBlock')) {
|
||||||
this.editor
|
|
||||||
.chain()
|
|
||||||
.command(({ tr }) => {
|
|
||||||
tr.insertText(TAB_CHAR);
|
|
||||||
return true;
|
|
||||||
})
|
|
||||||
.run();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"Mod-a": () => {
|
|
||||||
if (this.editor.isActive("codeBlock")) {
|
|
||||||
const { state } = this.editor;
|
const { state } = this.editor;
|
||||||
const { $from } = state.selection;
|
const { $from } = state.selection;
|
||||||
|
|
||||||
@@ -60,7 +48,7 @@ export const CustomCodeBlock = CodeBlock.extend<CodeBlockLowlightOptions>({
|
|||||||
|
|
||||||
for (depth = $from.depth; depth > 0; depth--) {
|
for (depth = $from.depth; depth > 0; depth--) {
|
||||||
const node = $from.node(depth);
|
const node = $from.node(depth);
|
||||||
if (node.type.name === "codeBlock") {
|
if (node.type.name === 'codeBlock') {
|
||||||
codeBlockNode = node;
|
codeBlockNode = node;
|
||||||
codeBlockPos = $from.start(depth) - 1;
|
codeBlockPos = $from.start(depth) - 1;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -0,0 +1,223 @@
|
|||||||
|
import { Extension } from '@tiptap/core';
|
||||||
|
import {
|
||||||
|
Plugin,
|
||||||
|
PluginKey,
|
||||||
|
type EditorState,
|
||||||
|
type Transaction,
|
||||||
|
} from '@tiptap/pm/state';
|
||||||
|
|
||||||
|
export type IndentOptions = {
|
||||||
|
types: string[];
|
||||||
|
min: number;
|
||||||
|
max: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
declare module '@tiptap/core' {
|
||||||
|
interface Commands<ReturnType> {
|
||||||
|
indent: {
|
||||||
|
indent: () => ReturnType;
|
||||||
|
outdent: () => ReturnType;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Containers whose descendants must never carry an `indent` attribute. These
|
||||||
|
// nodes own their own Tab semantics (list nesting, cell navigation, literal
|
||||||
|
// tab) and visually conflict with our indent padding, so paragraphs and
|
||||||
|
// headings inside them stay flat
|
||||||
|
const NON_INDENTABLE_ANCESTORS = new Set([
|
||||||
|
'listItem',
|
||||||
|
'taskItem',
|
||||||
|
'tableCell',
|
||||||
|
'tableHeader',
|
||||||
|
'codeBlock',
|
||||||
|
]);
|
||||||
|
|
||||||
|
const clampIndent = (value: number, min: number, max: number): number => {
|
||||||
|
if (!Number.isFinite(value)) return min;
|
||||||
|
return Math.max(min, Math.min(max, Math.trunc(value)));
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasNonIndentableAncestor = (
|
||||||
|
doc: EditorState['doc'],
|
||||||
|
pos: number,
|
||||||
|
): boolean => {
|
||||||
|
const $pos = doc.resolve(pos);
|
||||||
|
for (let depth = $pos.depth; depth >= 0; depth--) {
|
||||||
|
if (NON_INDENTABLE_ANCESTORS.has($pos.node(depth).type.name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Indent = Extension.create<IndentOptions>({
|
||||||
|
name: 'indent',
|
||||||
|
|
||||||
|
priority: 1000,
|
||||||
|
|
||||||
|
addOptions() {
|
||||||
|
return {
|
||||||
|
types: ['paragraph', 'heading'],
|
||||||
|
min: 0,
|
||||||
|
max: 8,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
addGlobalAttributes() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
types: this.options.types,
|
||||||
|
attributes: {
|
||||||
|
indent: {
|
||||||
|
default: this.options.min,
|
||||||
|
keepOnSplit: true,
|
||||||
|
parseHTML: (element) => {
|
||||||
|
const raw = element.getAttribute('data-indent');
|
||||||
|
if (raw === null) return this.options.min;
|
||||||
|
return clampIndent(
|
||||||
|
parseInt(raw, 10),
|
||||||
|
this.options.min,
|
||||||
|
this.options.max,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
renderHTML: (attributes) => {
|
||||||
|
const value = attributes.indent;
|
||||||
|
if (value <= this.options.min) return {};
|
||||||
|
return { 'data-indent': String(value) };
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
},
|
||||||
|
|
||||||
|
addCommands() {
|
||||||
|
return {
|
||||||
|
indent:
|
||||||
|
() =>
|
||||||
|
({ state, tr, dispatch }) => {
|
||||||
|
return updateIndent(state, tr, dispatch, this.options, +1);
|
||||||
|
},
|
||||||
|
outdent:
|
||||||
|
() =>
|
||||||
|
({ state, tr, dispatch }) => {
|
||||||
|
return updateIndent(state, tr, dispatch, this.options, -1);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
addKeyboardShortcuts() {
|
||||||
|
const isInIndentableBlock = (): boolean => {
|
||||||
|
const { $from } = this.editor.state.selection;
|
||||||
|
if (!this.options.types.includes($from.parent.type.name)) return false;
|
||||||
|
for (let depth = $from.depth - 1; depth >= 0; depth--) {
|
||||||
|
if (NON_INDENTABLE_ANCESTORS.has($from.node(depth).type.name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
Tab: () => {
|
||||||
|
if (!isInIndentableBlock()) return false;
|
||||||
|
this.editor.commands.indent();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
'Shift-Tab': () => {
|
||||||
|
if (!isInIndentableBlock()) return false;
|
||||||
|
this.editor.commands.outdent();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
Backspace: () => {
|
||||||
|
const { $from, empty } = this.editor.state.selection;
|
||||||
|
if (!empty) return false;
|
||||||
|
if ($from.parentOffset !== 0) return false;
|
||||||
|
if (!isInIndentableBlock()) return false;
|
||||||
|
if ($from.parent.attrs.indent <= this.options.min) return false;
|
||||||
|
this.editor.commands.outdent();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
addProseMirrorPlugins() {
|
||||||
|
const types = new Set(this.options.types);
|
||||||
|
const min = this.options.min;
|
||||||
|
|
||||||
|
return [
|
||||||
|
new Plugin({
|
||||||
|
key: new PluginKey('indentNormalizer'),
|
||||||
|
appendTransaction: (transactions, _oldState, newState) => {
|
||||||
|
if (!transactions.some((tr) => tr.docChanged)) return null;
|
||||||
|
|
||||||
|
const tr = newState.tr;
|
||||||
|
let modified = false;
|
||||||
|
|
||||||
|
newState.doc.descendants((node, pos) => {
|
||||||
|
// Containers: descend so we can find paragraph/heading children.
|
||||||
|
if (!types.has(node.type.name)) return true;
|
||||||
|
|
||||||
|
if (node.attrs.indent <= min) return false;
|
||||||
|
|
||||||
|
if (hasNonIndentableAncestor(newState.doc, pos)) {
|
||||||
|
tr.setNodeMarkup(
|
||||||
|
pos,
|
||||||
|
undefined,
|
||||||
|
{ ...node.attrs, indent: min },
|
||||||
|
node.marks,
|
||||||
|
);
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// paragraph/heading don't contain other paragraphs/headings —
|
||||||
|
// never descend into their inline content.
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!modified) return null;
|
||||||
|
// Normalisation must not show up as a separate undo step;
|
||||||
|
// otherwise undo would re-introduce the illegal indent.
|
||||||
|
return tr.setMeta('addToHistory', false);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
function updateIndent(
|
||||||
|
state: EditorState,
|
||||||
|
tr: Transaction,
|
||||||
|
dispatch: ((tr: Transaction) => void) | undefined,
|
||||||
|
options: IndentOptions,
|
||||||
|
delta: number,
|
||||||
|
): boolean {
|
||||||
|
const { selection } = state;
|
||||||
|
const { from, to } = selection;
|
||||||
|
const types = new Set(options.types);
|
||||||
|
let updated = false;
|
||||||
|
|
||||||
|
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||||
|
// Skip non-block nodes (text, inline atoms) up front.
|
||||||
|
if (!node.type.isBlock) return false;
|
||||||
|
|
||||||
|
// Don't descend into containers whose children must stay flat — handles
|
||||||
|
// multi-block selections that span across e.g. a list-item or table-cell.
|
||||||
|
if (NON_INDENTABLE_ANCESTORS.has(node.type.name)) return false;
|
||||||
|
|
||||||
|
if (!types.has(node.type.name)) return true;
|
||||||
|
|
||||||
|
const current = node.attrs.indent as number;
|
||||||
|
const next = clampIndent(current + delta, options.min, options.max);
|
||||||
|
if (next === current) return false;
|
||||||
|
|
||||||
|
tr.setNodeMarkup(pos, undefined, { ...node.attrs, indent: next });
|
||||||
|
updated = true;
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!updated) return false;
|
||||||
|
if (dispatch) dispatch(tr);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user