feat(widget): 添加 postSlug 配置以支持多语言/多路径评论聚合
- 在配置面板和核心逻辑中新增 postSlug 字段,允许自定义评论标识符 - 更新前端配置文档,说明 postSlug 的用途和默认行为 - 当未配置 postSlug 时,默认仍使用 window.location.pathname
This commit is contained in:
@@ -32,7 +32,7 @@ export class CWDComments {
|
||||
if (config.siteId) {
|
||||
this.config.siteId = config.siteId;
|
||||
}
|
||||
if (typeof window !== 'undefined') {
|
||||
if (typeof window !== 'undefined' && !this.config.postSlug) {
|
||||
this.config.postSlug = window.location.pathname;
|
||||
}
|
||||
if (typeof document !== 'undefined') {
|
||||
@@ -577,7 +577,7 @@ export class CWDComments {
|
||||
if (newConfig.siteId !== undefined) {
|
||||
this.config.siteId = newConfig.siteId;
|
||||
}
|
||||
if (typeof window !== 'undefined') {
|
||||
if (typeof window !== 'undefined' && !this.config.postSlug) {
|
||||
this.config.postSlug = window.location.pathname;
|
||||
}
|
||||
if (typeof document !== 'undefined') {
|
||||
|
||||
@@ -13,6 +13,7 @@ const DEFAULT_CONFIG = {
|
||||
apiBaseUrl: 'http://localhost:8788',
|
||||
siteId: 'cwd-dev-config',
|
||||
theme: 'light', // 默认 light / dark
|
||||
postSlug: '',
|
||||
};
|
||||
|
||||
let widgetInstance = null;
|
||||
@@ -49,11 +50,13 @@ function populateInputs(config) {
|
||||
const themeSelect = document.getElementById('theme');
|
||||
const avatarPrefixInput = document.getElementById('avatarPrefix');
|
||||
const siteIdInput = document.getElementById('siteId');
|
||||
const postSlugInput = document.getElementById('postSlug');
|
||||
|
||||
if (apiBaseUrlInput) apiBaseUrlInput.value = config.apiBaseUrl || DEFAULT_CONFIG.apiBaseUrl;
|
||||
if (themeSelect) themeSelect.value = config.theme || DEFAULT_CONFIG.theme;
|
||||
if (avatarPrefixInput) avatarPrefixInput.value = config.avatarPrefix || DEFAULT_CONFIG.avatarPrefix;
|
||||
if (siteIdInput) siteIdInput.value = config.siteId || DEFAULT_CONFIG.siteId || '';
|
||||
if (postSlugInput) postSlugInput.value = config.postSlug || DEFAULT_CONFIG.postSlug || '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +66,8 @@ function getConfigFromInputs() {
|
||||
const apiBaseUrl = document.getElementById('apiBaseUrl')?.value || DEFAULT_CONFIG.apiBaseUrl;
|
||||
const theme = document.getElementById('theme')?.value || DEFAULT_CONFIG.theme;
|
||||
const siteId = document.getElementById('siteId')?.value || DEFAULT_CONFIG.siteId || '';
|
||||
return { apiBaseUrl, theme, siteId };
|
||||
const postSlug = document.getElementById('postSlug')?.value || DEFAULT_CONFIG.postSlug || '';
|
||||
return { apiBaseUrl, theme, siteId, postSlug };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,6 +97,7 @@ async function initWidget() {
|
||||
el: '#comments',
|
||||
apiBaseUrl: config.apiBaseUrl,
|
||||
siteId: config.siteId,
|
||||
postSlug: config.postSlug,
|
||||
});
|
||||
widgetInstance.mount();
|
||||
} catch (error) {}
|
||||
@@ -153,7 +158,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
// 监听输入框变化,实时保存
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const inputs = ['apiBaseUrl', 'theme', 'siteId'];
|
||||
const inputs = ['apiBaseUrl', 'theme', 'siteId', 'postSlug'];
|
||||
inputs.forEach((id) => {
|
||||
const element = document.getElementById(id);
|
||||
if (element) {
|
||||
|
||||
Reference in New Issue
Block a user