Skip to content

Commit

Permalink
[#66861] expose a getUserUrl prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin committed Oct 4, 2024
1 parent 6f81505 commit b10a858
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ Here are the props you can pass to the MystEditor component:
- `topbar` *(default: true)* - whether to show the topbar
- `templateList` - path/url to a JSON file containing your document templates. For an example see `public/linkedtemplatelist.json`.
- `transforms` - [custom transforms](#custom-transforms)
- `getAvatar` *(default: (login) => `https://secure.gravatar.com/avatar/${login}?s=30&d=identicon`)* - a function that returns the avatar for a given username
- `getUserUrl` *(default: (login) => `#`)* - a function that returns a url to a web page with a users profile
It is used when an avatar is clicked.
- `collaboration` - options related to live collaboration:
- `enabled` *(default: false)*
- `wsUrl` *(example: ws://example:4444)* - url of the websocket server
Expand Down
8 changes: 6 additions & 2 deletions src/MystEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const MystEditor = ({
transforms = [],
// this will create a bogus random avatar when no specific getAvatar function is provided
getAvatar = (login) => `https://secure.gravatar.com/avatar/${login}?s=30&d=identicon`,
getUserUrl = (login) => "#",
backslashLineBreak = true,
parent,
syncScroll = false,
Expand All @@ -147,10 +148,13 @@ const MystEditor = ({
const text = useText({ initialText, transforms, customRoles, preview, backslashLineBreak, parent });

const [alert, setAlert] = useState(null);
const [users, setUsers] = useReducer((_, currentUsers) => currentUsers.map((u) => ({ ...u, avatarUrl: getAvatar(u.login) })), []);
const [users, setUsers] = useReducer(
(_, currentUsers) => currentUsers.map((u) => ({ ...u, avatarUrl: getAvatar(u.login), userUrl: getUserUrl(u.login) })),
[],
);

const { provider, undoManager, ytext, ydoc, ready, error } = useCollaboration(collaboration);
const ycomments = useComments(ydoc, provider, getAvatar);
const ycomments = useComments(ydoc, provider, getAvatar, getUserUrl);

const alertFor = (alertText, secs) => {
setAlert(alertText);
Expand Down
9 changes: 6 additions & 3 deletions src/comments/ycomments.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const newRandomCommentId = () => "comment-" + Math.random().toString().replace("
* into Y.Array are immutable (if they are not Y.js primitives).
*/
export class CommentLineAuthors {
constructor(/** @type {Y.Doc} */ ydoc, provider, getAvatar, commentId) {
constructor(/** @type {Y.Doc} */ ydoc, provider, getAvatar, getUserUrl, commentId) {
this.user = provider.awareness.getLocalState().user;
/** @type {Y.Array<Y.Map<{name: string, color: string, avatar?: string }>>} */
this.lineAuthors = ydoc.getArray(commentId + "/commentLineAuthors");
this.ydoc = ydoc;
this.getAvatar = getAvatar;
this.getUserUrl = getUserUrl;
this.commentId = commentId;
}

Expand All @@ -46,6 +47,7 @@ export class CommentLineAuthors {
if (!authorData) return;

authorData.avatar = this.getAvatar(authorData.name);
authorData.url = this.getUserUrl(authorData.name);
return authorData;
}

Expand Down Expand Up @@ -269,10 +271,11 @@ export class YComments {
* @param {Y.Doc} ydoc
* @param {WebsocketProvider} provider
*/
constructor(ydoc, provider, getAvatar) {
constructor(ydoc, provider, getAvatar, getUserUrl) {
this.ydoc = ydoc;
this.provider = provider;
this.getAvatar = getAvatar;
this.getUserUrl = getUserUrl;

/** @type {EditorView} The main codemirror instance */
this.mainCodeMirror = null;
Expand Down Expand Up @@ -304,7 +307,7 @@ export class YComments {
}

lineAuthors(commentId) {
return new CommentLineAuthors(this.ydoc, this.provider, this.getAvatar, commentId);
return new CommentLineAuthors(this.ydoc, this.provider, this.getAvatar, this.getUserUrl, commentId);
}

positions() {
Expand Down
6 changes: 4 additions & 2 deletions src/components/Avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ const AvatarsWrapper = styled.div`
}
`;

export const Avatar = ({ login, color, avatarUrl }) =>
html` <img src=${avatarUrl} key=${login} title=${login} class="avatar" style="border-color: ${color}" />`;
export const Avatar = ({ login, color, avatarUrl, userUrl }) =>
html`<a href=${userUrl || "#"} target="_blank"
><img src=${avatarUrl} key=${login} title=${login} class="avatar" style="border-color: ${color}"
/></a>`;

const AvatarPlaceholder = ({ n, usernames }) =>
html` <${AvatarsWrapper} title=${usernames}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ResolvedComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ const ResolvedComment = ({ c, authors, ycomments, content }) => {
<${CommentContainer} className="resolved-comment" color=${authors.get(1).color}>
<${CommentTopbar}>
<${FlexRow}>
<${Avatar} login=${authors.get(1).name} color=${authors.get(1).color} avatarUrl=${authors.get(1).avatar} />
<${Avatar} login=${authors.get(1).name} color=${authors.get(1).color} avatarUrl=${authors.get(1).avatar} userUrl=${authors.get(1).url} />
<${CommentAuthor}>${authors.get(1).name}<//>
<//>
<${FlexRow}>
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useComments.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useMemo } from "preact/hooks";
import { YComments } from "../comments/ycomments";

export default function useComments(ydoc, provider, getAvatar) {
export default function useComments(ydoc, provider, getAvatar, getUserUrl) {
if (!ydoc || !provider) return null;
return useMemo(() => {
const ycomments = new YComments(ydoc, provider, getAvatar);
const ycomments = new YComments(ydoc, provider, getAvatar, getUserUrl);
window.myst_editor.ycomments = ycomments;
return ycomments;
}, []);
Expand Down

0 comments on commit b10a858

Please sign in to comment.