@blocklet/discuss-kit-post 2.1.144 → 2.1.146

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export declare const api: import('axios').AxiosInstance;
2
+ export declare const fetchPost: (postId: string, locale: string) => Promise<any>;
3
+ export declare const updatePostContent: (id: string, payload: {
4
+ title: string;
5
+ content: string;
6
+ contentVersion: number;
7
+ locale?: string;
8
+ }) => Promise<any>;
package/dist/index.es.js CHANGED
@@ -1,35 +1,108 @@
1
1
  var _a, _b;
2
- import { jsx } from "react/jsx-runtime";
3
- import { Box } from "@mui/material";
2
+ import { jsx, jsxs } from "react/jsx-runtime";
3
+ import { Box, IconButton, Button } from "@mui/material";
4
4
  import { useRequest } from "ahooks";
5
- import { withQuery, joinURL } from "ufo";
6
5
  import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
7
- import { EditorPreview, InternalThemeProvider } from "@blocklet/discuss-kit-ux";
8
- const discussKitBlocklet = (_b = (_a = window.blocklet) == null ? void 0 : _a.componentMountPoints) == null ? void 0 : _b.find((x) => x.name === "did-comments");
9
- const discussKitMountPoint = discussKitBlocklet == null ? void 0 : discussKitBlocklet.mountPoint;
6
+ import { SessionContext } from "@arcblock/did-connect/lib/Session";
7
+ import { useContext, useState } from "react";
8
+ import { EditorPreview, InternalThemeProvider, LazyEditor } from "@blocklet/discuss-kit-ux";
9
+ import { Edit } from "@mui/icons-material";
10
+ import { createAxios } from "@blocklet/js-sdk";
11
+ import { joinURL, withQuery } from "ufo";
12
+ const discussKitService = (_b = (_a = window.blocklet) == null ? void 0 : _a.componentMountPoints) == null ? void 0 : _b.find(
13
+ (x) => x.did === "z8ia1WEiBZ7hxURf6LwH21Wpg99vophFwSJdu"
14
+ );
15
+ const discussKitPrefix = discussKitService ? joinURL(discussKitService.mountPoint, "/") : "";
16
+ const discussKitApiPrefix = joinURL(discussKitPrefix, "/api/");
17
+ const api = createAxios({
18
+ baseURL: discussKitApiPrefix,
19
+ timeout: 2e4
20
+ });
10
21
  const fetchPost = async (postId, locale) => {
11
- const url = withQuery(joinURL(discussKitMountPoint || "/", "/api/posts/slug", postId), { locale });
22
+ const url = withQuery(joinURL("/posts/slug", postId), { locale });
12
23
  try {
13
- const result = await fetch(url).then((res) => res.json());
24
+ const result = await api.get(url).then((res) => res.data);
14
25
  return result;
15
26
  } catch (e) {
16
27
  console.error(e);
17
28
  return null;
18
29
  }
19
30
  };
20
- function InternalPost({ postId, ...rest }) {
31
+ const updatePostContent = async (id, payload) => {
32
+ const { data } = await api.put(`/posts/${id}/content`, payload);
33
+ return data;
34
+ };
35
+ function InternalPost({ postId, sx, ...rest }) {
36
+ var _a2, _b2;
21
37
  const { locale } = useLocaleContext();
22
- const { data: post, loading } = useRequest(() => fetchPost(postId, locale), { refreshDeps: [postId, locale] });
38
+ const sessionCtx = useContext(SessionContext);
39
+ const isAdmin = ["admin", "owner"].includes((_b2 = (_a2 = sessionCtx == null ? void 0 : sessionCtx.session) == null ? void 0 : _a2.user) == null ? void 0 : _b2.role);
40
+ const [editable, setEditable] = useState(false);
41
+ const {
42
+ data: post,
43
+ mutate,
44
+ loading
45
+ } = useRequest(() => fetchPost(postId, locale), { refreshDeps: [postId, locale] });
23
46
  if (loading) {
24
47
  return EditorPreview.fallback;
25
48
  }
26
49
  if (!post) {
27
50
  return null;
28
51
  }
29
- return /* @__PURE__ */ jsx(InternalThemeProvider, { children: /* @__PURE__ */ jsx(Box, { ...rest, children: /* @__PURE__ */ jsx(EditorPreview, { content: post.content || "" }) }) });
52
+ const handleSave = async () => {
53
+ const updated = await updatePostContent(postId, {
54
+ title: post.title,
55
+ content: post.content,
56
+ locale: post.locale,
57
+ contentVersion: post.contentVersion
58
+ });
59
+ mutate((prev) => ({
60
+ ...prev,
61
+ content: updated.content,
62
+ contentVersion: updated.contentVersion
63
+ }));
64
+ setEditable(false);
65
+ };
66
+ const mergedSx = [
67
+ {
68
+ position: "relative",
69
+ "&:hover .post-component-edit-button": {
70
+ display: "block"
71
+ }
72
+ },
73
+ ...Array.isArray(sx) ? sx : [sx]
74
+ ];
75
+ return /* @__PURE__ */ jsx(InternalThemeProvider, { children: /* @__PURE__ */ jsxs(Box, { sx: mergedSx, ...rest, children: [
76
+ isAdmin && !editable && /* @__PURE__ */ jsx(
77
+ IconButton,
78
+ {
79
+ className: "post-component-edit-button",
80
+ color: "primary",
81
+ sx: { display: "none", position: "absolute", top: 0, right: 0, zIndex: 1e3 },
82
+ onClick: () => setEditable(true),
83
+ children: /* @__PURE__ */ jsx(Edit, {})
84
+ }
85
+ ),
86
+ editable ? /* @__PURE__ */ jsxs(Box, { sx: {}, children: [
87
+ /* @__PURE__ */ jsxs(Box, { sx: { mb: 1, display: "flex", gap: 1 }, children: [
88
+ /* @__PURE__ */ jsx(Button, { variant: "contained", color: "primary", onClick: () => handleSave(), children: "Save" }),
89
+ /* @__PURE__ */ jsx(Button, { variant: "outlined", onClick: () => setEditable(false), children: "Cancel" })
90
+ ] }),
91
+ /* @__PURE__ */ jsx(
92
+ LazyEditor,
93
+ {
94
+ initialContent: post.content || "",
95
+ onChange: ({ content }) => mutate((prev) => ({
96
+ ...prev,
97
+ content
98
+ }))
99
+ }
100
+ )
101
+ ] }) : /* @__PURE__ */ jsx(EditorPreview, { content: post.content || "" })
102
+ ] }) });
30
103
  }
31
104
  function Post(props) {
32
- if (!discussKitMountPoint) {
105
+ if (!discussKitService) {
33
106
  return null;
34
107
  }
35
108
  return /* @__PURE__ */ jsx(InternalPost, { ...props });
package/dist/index.umd.js CHANGED
@@ -1,33 +1,102 @@
1
1
  (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react/jsx-runtime"), require("@mui/material"), require("ahooks"), require("ufo"), require("@arcblock/ux/lib/Locale/context"), require("@blocklet/discuss-kit-ux")) : typeof define === "function" && define.amd ? define(["exports", "react/jsx-runtime", "@mui/material", "ahooks", "ufo", "@arcblock/ux/lib/Locale/context", "@blocklet/discuss-kit-ux"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.DiscussKitPost = {}, global.jsxRuntime, global.material, global.ahooks, global.ufo, global.context, global.discussKitUx));
3
- })(this, function(exports2, jsxRuntime, material, ahooks, ufo, context, discussKitUx) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react/jsx-runtime"), require("@mui/material"), require("ahooks"), require("@arcblock/ux/lib/Locale/context"), require("@arcblock/did-connect/lib/Session"), require("react"), require("@blocklet/discuss-kit-ux"), require("@mui/icons-material"), require("@blocklet/js-sdk"), require("ufo")) : typeof define === "function" && define.amd ? define(["exports", "react/jsx-runtime", "@mui/material", "ahooks", "@arcblock/ux/lib/Locale/context", "@arcblock/did-connect/lib/Session", "react", "@blocklet/discuss-kit-ux", "@mui/icons-material", "@blocklet/js-sdk", "ufo"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.DiscussKitPost = {}, global.jsxRuntime, global.material, global.ahooks, global.context, global.Session, global.react, global.discussKitUx, global.iconsMaterial, global.jsSdk, global.ufo));
3
+ })(this, function(exports2, jsxRuntime, material, ahooks, context, Session, react, discussKitUx, iconsMaterial, jsSdk, ufo) {
4
4
  "use strict";
5
5
  var _a, _b;
6
- const discussKitBlocklet = (_b = (_a = window.blocklet) == null ? void 0 : _a.componentMountPoints) == null ? void 0 : _b.find((x) => x.name === "did-comments");
7
- const discussKitMountPoint = discussKitBlocklet == null ? void 0 : discussKitBlocklet.mountPoint;
6
+ const discussKitService = (_b = (_a = window.blocklet) == null ? void 0 : _a.componentMountPoints) == null ? void 0 : _b.find(
7
+ (x) => x.did === "z8ia1WEiBZ7hxURf6LwH21Wpg99vophFwSJdu"
8
+ );
9
+ const discussKitPrefix = discussKitService ? ufo.joinURL(discussKitService.mountPoint, "/") : "";
10
+ const discussKitApiPrefix = ufo.joinURL(discussKitPrefix, "/api/");
11
+ const api = jsSdk.createAxios({
12
+ baseURL: discussKitApiPrefix,
13
+ timeout: 2e4
14
+ });
8
15
  const fetchPost = async (postId, locale) => {
9
- const url = ufo.withQuery(ufo.joinURL(discussKitMountPoint || "/", "/api/posts/slug", postId), { locale });
16
+ const url = ufo.withQuery(ufo.joinURL("/posts/slug", postId), { locale });
10
17
  try {
11
- const result = await fetch(url).then((res) => res.json());
18
+ const result = await api.get(url).then((res) => res.data);
12
19
  return result;
13
20
  } catch (e) {
14
21
  console.error(e);
15
22
  return null;
16
23
  }
17
24
  };
18
- function InternalPost({ postId, ...rest }) {
25
+ const updatePostContent = async (id, payload) => {
26
+ const { data } = await api.put(`/posts/${id}/content`, payload);
27
+ return data;
28
+ };
29
+ function InternalPost({ postId, sx, ...rest }) {
30
+ var _a2, _b2;
19
31
  const { locale } = context.useLocaleContext();
20
- const { data: post, loading } = ahooks.useRequest(() => fetchPost(postId, locale), { refreshDeps: [postId, locale] });
32
+ const sessionCtx = react.useContext(Session.SessionContext);
33
+ const isAdmin = ["admin", "owner"].includes((_b2 = (_a2 = sessionCtx == null ? void 0 : sessionCtx.session) == null ? void 0 : _a2.user) == null ? void 0 : _b2.role);
34
+ const [editable, setEditable] = react.useState(false);
35
+ const {
36
+ data: post,
37
+ mutate,
38
+ loading
39
+ } = ahooks.useRequest(() => fetchPost(postId, locale), { refreshDeps: [postId, locale] });
21
40
  if (loading) {
22
41
  return discussKitUx.EditorPreview.fallback;
23
42
  }
24
43
  if (!post) {
25
44
  return null;
26
45
  }
27
- return /* @__PURE__ */ jsxRuntime.jsx(discussKitUx.InternalThemeProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(material.Box, { ...rest, children: /* @__PURE__ */ jsxRuntime.jsx(discussKitUx.EditorPreview, { content: post.content || "" }) }) });
46
+ const handleSave = async () => {
47
+ const updated = await updatePostContent(postId, {
48
+ title: post.title,
49
+ content: post.content,
50
+ locale: post.locale,
51
+ contentVersion: post.contentVersion
52
+ });
53
+ mutate((prev) => ({
54
+ ...prev,
55
+ content: updated.content,
56
+ contentVersion: updated.contentVersion
57
+ }));
58
+ setEditable(false);
59
+ };
60
+ const mergedSx = [
61
+ {
62
+ position: "relative",
63
+ "&:hover .post-component-edit-button": {
64
+ display: "block"
65
+ }
66
+ },
67
+ ...Array.isArray(sx) ? sx : [sx]
68
+ ];
69
+ return /* @__PURE__ */ jsxRuntime.jsx(discussKitUx.InternalThemeProvider, { children: /* @__PURE__ */ jsxRuntime.jsxs(material.Box, { sx: mergedSx, ...rest, children: [
70
+ isAdmin && !editable && /* @__PURE__ */ jsxRuntime.jsx(
71
+ material.IconButton,
72
+ {
73
+ className: "post-component-edit-button",
74
+ color: "primary",
75
+ sx: { display: "none", position: "absolute", top: 0, right: 0, zIndex: 1e3 },
76
+ onClick: () => setEditable(true),
77
+ children: /* @__PURE__ */ jsxRuntime.jsx(iconsMaterial.Edit, {})
78
+ }
79
+ ),
80
+ editable ? /* @__PURE__ */ jsxRuntime.jsxs(material.Box, { sx: {}, children: [
81
+ /* @__PURE__ */ jsxRuntime.jsxs(material.Box, { sx: { mb: 1, display: "flex", gap: 1 }, children: [
82
+ /* @__PURE__ */ jsxRuntime.jsx(material.Button, { variant: "contained", color: "primary", onClick: () => handleSave(), children: "Save" }),
83
+ /* @__PURE__ */ jsxRuntime.jsx(material.Button, { variant: "outlined", onClick: () => setEditable(false), children: "Cancel" })
84
+ ] }),
85
+ /* @__PURE__ */ jsxRuntime.jsx(
86
+ discussKitUx.LazyEditor,
87
+ {
88
+ initialContent: post.content || "",
89
+ onChange: ({ content }) => mutate((prev) => ({
90
+ ...prev,
91
+ content
92
+ }))
93
+ }
94
+ )
95
+ ] }) : /* @__PURE__ */ jsxRuntime.jsx(discussKitUx.EditorPreview, { content: post.content || "" })
96
+ ] }) });
28
97
  }
29
98
  function Post(props) {
30
- if (!discussKitMountPoint) {
99
+ if (!discussKitService) {
31
100
  return null;
32
101
  }
33
102
  return /* @__PURE__ */ jsxRuntime.jsx(InternalPost, { ...props });
@@ -0,0 +1,3 @@
1
+ export declare const discussKitService: ComponentMountPoint | undefined;
2
+ export declare const discussKitPrefix: string;
3
+ export declare const discussKitApiPrefix: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/discuss-kit-post",
3
- "version": "2.1.144",
3
+ "version": "2.1.146",
4
4
  "files": [
5
5
  "dist"
6
6
  ],
@@ -18,9 +18,11 @@
18
18
  "access": "public"
19
19
  },
20
20
  "dependencies": {
21
+ "@blocklet/js-sdk": "^1.16.37",
22
+ "@mui/icons-material": "^5.16.7",
21
23
  "ahooks": "^3.8.1",
22
24
  "ufo": "1.3.1",
23
- "@blocklet/discuss-kit-ux": "^2.1.144"
25
+ "@blocklet/discuss-kit-ux": "^2.1.146"
24
26
  },
25
27
  "peerDependencies": {
26
28
  "@arcblock/did-connect": "^2.10.36",