@rspress-theme-anatole/theme-default 0.7.14 → 0.7.16

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.
Files changed (2) hide show
  1. package/dist/bundle.js +180 -0
  2. package/package.json +3 -3
package/dist/bundle.js CHANGED
@@ -4576,6 +4576,182 @@ function NavVersions() {
4576
4576
  })
4577
4577
  });
4578
4578
  }
4579
+ // Utility functions for cookie management
4580
+ function setCookie(name, value, days = 7) {
4581
+ const expires = new Date();
4582
+ expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
4583
+ document.cookie = `${name}=${encodeURIComponent(JSON.stringify(value))};expires=${expires.toUTCString()};path=/`;
4584
+ }
4585
+
4586
+ function getCookie(name) {
4587
+ const nameEQ = name + "=";
4588
+ const ca = document.cookie.split(';');
4589
+ for (let i = 0; i < ca.length; i++) {
4590
+ let c = ca[i];
4591
+ while (c.charAt(0) === ' ') c = c.substring(1, c.length);
4592
+ if (c.indexOf(nameEQ) === 0) {
4593
+ try {
4594
+ return JSON.parse(decodeURIComponent(c.substring(nameEQ.length, c.length)));
4595
+ } catch (e) {
4596
+ return null;
4597
+ }
4598
+ }
4599
+ }
4600
+ return null;
4601
+ }
4602
+
4603
+ function deleteCookie(name) {
4604
+ document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/`;
4605
+ }
4606
+
4607
+ // User Authentication Component
4608
+ function UserAuth() {
4609
+ const { siteData } = (0, __WEBPACK_EXTERNAL_MODULE__rspress_runtime_0abd3046__.usePageData)();
4610
+ const [user, setUser] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
4611
+ const [showDropdown, setShowDropdown] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
4612
+ const dropdownRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
4613
+
4614
+ // Get sign in URL from site config
4615
+ const signInUrl = siteData?.themeConfig?.auth?.signInUrl;
4616
+
4617
+ (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(() => {
4618
+ // Check for user cookie on component mount
4619
+ const userData = getCookie('user_auth');
4620
+ if (userData) {
4621
+ setUser(userData);
4622
+ }
4623
+
4624
+ // Listen for storage events to sync across tabs
4625
+ const handleStorageChange = () => {
4626
+ const userData = getCookie('user_auth');
4627
+ setUser(userData);
4628
+ };
4629
+
4630
+ window.addEventListener('storage', handleStorageChange);
4631
+ return () => window.removeEventListener('storage', handleStorageChange);
4632
+ }, []);
4633
+
4634
+ (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(() => {
4635
+ // Close dropdown when clicking outside
4636
+ function handleClickOutside(event) {
4637
+ if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
4638
+ setShowDropdown(false);
4639
+ }
4640
+ }
4641
+
4642
+ document.addEventListener('mousedown', handleClickOutside);
4643
+ return () => document.removeEventListener('mousedown', handleClickOutside);
4644
+ }, []);
4645
+
4646
+ const handleSignIn = async () => {
4647
+ if (!signInUrl) {
4648
+ console.warn('Sign in URL not configured');
4649
+ return;
4650
+ }
4651
+
4652
+ try {
4653
+ // Open sign in URL in new window/tab
4654
+ const authWindow = window.open(signInUrl, '_blank', 'width=600,height=600');
4655
+
4656
+ // Listen for messages from auth window
4657
+ const handleMessage = (event) => {
4658
+ if (event.origin !== new URL(signInUrl).origin) return;
4659
+
4660
+ if (event.data && event.data.type === 'AUTH_SUCCESS') {
4661
+ const userData = {
4662
+ username: event.data.username,
4663
+ avatar: event.data.avatar
4664
+ };
4665
+
4666
+ setCookie('user_auth', userData);
4667
+ setUser(userData);
4668
+ authWindow.close();
4669
+
4670
+ // Remove event listener
4671
+ window.removeEventListener('message', handleMessage);
4672
+ }
4673
+ };
4674
+
4675
+ window.addEventListener('message', handleMessage);
4676
+
4677
+ // Fallback: poll for cookie changes (in case postMessage isn't used)
4678
+ const pollInterval = setInterval(() => {
4679
+ if (authWindow.closed) {
4680
+ clearInterval(pollInterval);
4681
+ window.removeEventListener('message', handleMessage);
4682
+
4683
+ // Check if user data was set
4684
+ const userData = getCookie('user_auth');
4685
+ if (userData) {
4686
+ setUser(userData);
4687
+ }
4688
+ }
4689
+ }, 1000);
4690
+
4691
+ } catch (error) {
4692
+ console.error('Sign in error:', error);
4693
+ }
4694
+ };
4695
+
4696
+ const handleSignOut = () => {
4697
+ deleteCookie('user_auth');
4698
+ setUser(null);
4699
+ setShowDropdown(false);
4700
+ };
4701
+
4702
+ if (!user) {
4703
+ return (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("button", {
4704
+ onClick: handleSignIn,
4705
+ className: "py-2 px-6 text-sm font-medium text-white hover:text-text-2 transition-colors duration-200 border border-gray-300 rounded-md hover:border-gray-400",
4706
+ style: {
4707
+ backgroundColor: 'rgb(239, 72, 61)',
4708
+ color: 'white',
4709
+ },
4710
+ children: "Sign in"
4711
+ });
4712
+ }
4713
+
4714
+ return (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("div", {
4715
+ className: "relative",
4716
+ ref: dropdownRef,
4717
+ children: [
4718
+ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("button", {
4719
+ onClick: () => setShowDropdown(!showDropdown),
4720
+ className: "flex items-center gap-2 px-3 py-2 text-sm font-medium text-text-1 hover:text-text-2 transition-colors duration-200 rounded-md hover:bg-gray-100 dark:hover:bg-gray-800",
4721
+ children: [
4722
+ user.avatar && (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("img", {
4723
+ src: user.avatar.startsWith('data:') ? user.avatar : `data:image/png;base64,${user.avatar}`,
4724
+ alt: "User Avatar",
4725
+ className: "w-6 h-6 rounded-full object-cover"
4726
+ }),
4727
+ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("span", {
4728
+ children: user.username
4729
+ }),
4730
+ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("svg", {
4731
+ className: `w-4 h-4 transition-transform ${showDropdown ? 'rotate-180' : ''}`,
4732
+ fill: "none",
4733
+ stroke: "currentColor",
4734
+ viewBox: "0 0 24 24",
4735
+ children: (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("path", {
4736
+ strokeLinecap: "round",
4737
+ strokeLinejoin: "round",
4738
+ strokeWidth: 2,
4739
+ d: "M19 9l-7 7-7-7"
4740
+ })
4741
+ })
4742
+ ]
4743
+ }),
4744
+ showDropdown && (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("div", {
4745
+ className: "absolute right-0 mt-2 w-48 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-md shadow-lg z-50",
4746
+ children: (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("button", {
4747
+ onClick: handleSignOut,
4748
+ className: "w-full text-left px-4 py-2 text-sm text-text-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-md",
4749
+ children: "Sign out"
4750
+ })
4751
+ })
4752
+ ]
4753
+ });
4754
+ }
4579
4755
  const DEFAULT_NAV_POSITION = 'right';
4580
4756
  function Nav(props) {
4581
4757
  const { beforeNavTitle, afterNavTitle, beforeNav, afterNavMenu, navTitle } = props;
@@ -4669,6 +4845,10 @@ function Nav(props) {
4669
4845
  }),
4670
4846
  hasSocialLinks && (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)(SocialLinks, {
4671
4847
  socialLinks: socialLinks
4848
+ }),
4849
+ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("div", {
4850
+ className: "ml-2",
4851
+ children: (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)(UserAuth, {})
4672
4852
  })
4673
4853
  ]
4674
4854
  })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rspress-theme-anatole/theme-default",
3
3
  "author": "Anatole Tong",
4
- "version": "0.7.14",
4
+ "version": "0.7.16",
5
5
  "license": "MIT",
6
6
  "sideEffects": [
7
7
  "*.css",
@@ -21,8 +21,8 @@
21
21
  "types": "./dist/bundle.d.ts",
22
22
  "dependencies": {
23
23
  "@mdx-js/react": "2.3.0",
24
- "@rspress-theme-anatole/rspress-plugin-mermaid": "0.7.14",
25
- "@rspress-theme-anatole/shared": "0.7.14",
24
+ "@rspress-theme-anatole/rspress-plugin-mermaid": "0.7.16",
25
+ "@rspress-theme-anatole/shared": "0.7.16",
26
26
  "@rspress/runtime": "1.43.8",
27
27
  "body-scroll-lock": "4.0.0-beta.0",
28
28
  "copy-to-clipboard": "^3.3.3",