@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
130 lines (127 loc) • 5.43 kB
JavaScript
// packages/editor/src/components/collaborators-presence/index.tsx
import { Button } from "@wordpress/components";
import { useMemo, useState } from "@wordpress/element";
import {
privateApis
} from "@wordpress/core-data";
import { __, sprintf } from "@wordpress/i18n";
import Avatar from "./avatar/index.mjs";
import AvatarGroup from "./avatar-group/index.mjs";
import { CollaboratorsList } from "./list.mjs";
import { unlock } from "../../lock-unlock.mjs";
import { getAvatarUrl } from "../collaborators-overlay/get-avatar-url.mjs";
import { getAvatarBorderColor } from "../collab-sidebar/utils.mjs";
import { createCursorRegistry } from "../collaborators-overlay/cursor-registry.mjs";
// packages/editor/src/components/collaborators-presence/styles/collaborators-presence.scss
if (typeof document !== "undefined" && process.env.NODE_ENV !== "test" && !document.head.querySelector("style[data-wp-hash='5ffe927286']")) {
const style = document.createElement("style");
style.setAttribute("data-wp-hash", "5ffe927286");
style.appendChild(document.createTextNode(".editor-collaborators-presence{align-items:center;background:#f0f0f0;border-radius:4px;display:flex;flex-shrink:0;height:32px;margin-right:8px}.editor-collaborators-presence:has(.is-pressed),.editor-collaborators-presence:hover{background-color:#e0e0e0}.editor-collaborators-presence__button.editor-collaborators-presence__button.components-button{align-items:center;background:#0000;border-radius:4px;box-sizing:border-box;color:#2f2f2f;cursor:pointer;display:flex;height:100%;padding:4px;position:relative}.editor-collaborators-presence__button.editor-collaborators-presence__button.components-button.is-pressed,.editor-collaborators-presence__button.editor-collaborators-presence__button.components-button.is-pressed:hover,.editor-collaborators-presence__button.editor-collaborators-presence__button.components-button:hover{background:#0000;color:#2f2f2f}.editor-collaborators-presence__button.editor-collaborators-presence__button.components-button:focus:not(:active){box-shadow:inset 0 0 0 var(--wp-admin-border-width-focus,2px) var(--wp-admin-theme-color,#007cba);outline:none}"));
document.head.appendChild(style);
}
// packages/editor/src/components/collaborators-presence/index.tsx
import { CollaboratorsOverlay } from "../collaborators-overlay/index.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var { useActiveCollaborators } = unlock(privateApis);
function CollaboratorsPresence({
postId,
postType
}) {
const activeCollaborators = useActiveCollaborators(
postId,
postType
);
const otherActiveCollaborators = activeCollaborators.filter(
(c) => !c.isMe
);
const collaboratorsForList = useMemo(() => {
return [...activeCollaborators].sort((a, b) => {
if (a.isMe && !b.isMe) {
return -1;
}
if (!a.isMe && b.isMe) {
return 1;
}
return 0;
});
}, [activeCollaborators]);
const [cursorRegistry] = useState(createCursorRegistry);
const [isPopoverVisible, setIsPopoverVisible] = useState(false);
const [popoverAnchor, setPopoverAnchor] = useState(
null
);
if (otherActiveCollaborators.length === 0) {
return null;
}
const me = activeCollaborators.find((c) => c.isMe);
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsxs("div", { className: "editor-collaborators-presence", children: [
/* @__PURE__ */ jsx(
Button,
{
__next40pxDefaultSize: true,
className: "editor-collaborators-presence__button",
onClick: () => setIsPopoverVisible(!isPopoverVisible),
isPressed: isPopoverVisible,
ref: setPopoverAnchor,
"aria-label": sprintf(
// translators: %d: number of online collaborators.
__("Collaborators list, %d online"),
collaboratorsForList.length
),
children: /* @__PURE__ */ jsxs(AvatarGroup, { max: 4, children: [
me && /* @__PURE__ */ jsx(
Avatar,
{
src: getAvatarUrl(
me.collaboratorInfo.avatar_urls
),
name: me.collaboratorInfo.name,
borderColor: "var(--wp-admin-theme-color)",
size: "small"
},
me.clientId
),
otherActiveCollaborators.map(
(collaboratorState) => /* @__PURE__ */ jsx(
Avatar,
{
src: getAvatarUrl(
collaboratorState.collaboratorInfo.avatar_urls
),
name: collaboratorState.collaboratorInfo.name,
borderColor: getAvatarBorderColor(
collaboratorState.collaboratorInfo.id
),
size: "small"
},
collaboratorState.clientId
)
)
] })
}
),
isPopoverVisible && /* @__PURE__ */ jsx(
CollaboratorsList,
{
activeCollaborators: collaboratorsForList,
popoverAnchor,
setIsPopoverVisible,
cursorRegistry
}
)
] }),
/* @__PURE__ */ jsx(
CollaboratorsOverlay,
{
postId,
postType,
cursorRegistry
}
)
] });
}
export {
CollaboratorsPresence
};
//# sourceMappingURL=index.mjs.map