期望:在书签原有内容基础上进行 😀 的添加;删除仅删除 😀,保留其他内容。
相关帖子
-
wilsons •付费者 捐赠者
试试这个是否想要的效果。
由于 Ctrl+R 是替换文本,建议改成 Ctrl+T 或其他,可通过参数配置修改,详情见参数说明。
// 通过快捷键给块书签添加删除标签 // see https://ld246.com/article/1757393544265 (()=>{ // 按键字母,由于Ctrl+R是替换,建议改成别的比如Ctrl+T const pressLetter = 'R'; // 必须大写 // 添加的表情 const emojis = '😀'; // 表情位置,prefix 开头,suffix 结尾 const emojisPosition = 'prefix'; document.addEventListener('keydown', async (event) => { // 检查是否按下了 Ctrl+R // 这里推荐用 Ctrl + T,只需要把下行的 KeyR 改为 KeyT 即可 if ((event.ctrlKey || event.metaKey) && event.code === 'Key'+pressLetter && !event.shiftKey && !event.altKey) { const cursorElement = getCursorElement(); if(!cursorElement) return; const cursorBlock = cursorElement.closest('.protyle-wysiwyg [data-node-id][data-type]'); if(!cursorBlock) return; const blockId = cursorBlock?.dataset?.nodeId; if(!blockId) return; event.preventDefault(); event.stopPropagation(); // 获取书签 const data = await requestApi('/api/attr/getBlockAttrs', { "id": blockId }); let bookmark = data?.data?.bookmark || ''; // 添加删除表情 const hasEmojis = bookmark.includes(emojis); bookmark = bookmark.replace(emojis, ''); if(!hasEmojis) bookmark = emojisPosition === 'prefix' ? emojis + bookmark : bookmark + emojis; requestApi('/api/attr/setBlockAttrs', { "id": blockId, "attrs": { "bookmark": bookmark } }); } }, true); function getCursorElement() { const selection = window.getSelection(); if (selection.rangeCount > 0) { const range = selection.getRangeAt(0); // 获取选择范围的起始位置所在的节点 const startContainer = range.startContainer; // 如果起始位置是文本节点,返回其父元素节点 const cursorElement = startContainer.nodeType === Node.TEXT_NODE ? startContainer.parentElement : startContainer; return cursorElement; } return null; } async function requestApi(url, data, method = 'POST') { return await (await fetch(url, {method: method, body: JSON.stringify(data||{})})).json(); } })();效果
-
完整版
// 通过快捷键给块书签添加删除标签 // see https://ld246.com/article/1757393544265 (()=>{ // 按键字母,由于Ctrl+R是替换,建议改成别的比如Ctrl+T const pressLetter = 'R'; // 必须大写 // 添加的表情 const emojis = '😀'; // 表情位置,prefix 开头,suffix 结尾 const emojisPosition = 'prefix'; document.addEventListener('keydown', async (event) => { // 检查是否按下了 Ctrl+R // 这里推荐用 Ctrl + T,只需要把下行的 KeyR 改为 KeyT 即可 if ((event.ctrlKey || event.metaKey) && event.code === 'Key'+pressLetter && !event.shiftKey && !event.altKey) { const cursorElement = getCursorElement(); if(!cursorElement) return; const cursorBlock = cursorElement.closest('.protyle-wysiwyg [data-node-id][data-type]'); if(!cursorBlock) return; const blockId = cursorBlock?.dataset?.nodeId; if(!blockId) return; event.preventDefault(); event.stopPropagation(); // 获取书签 const data = await requestApi('/api/attr/getBlockAttrs', { "id": blockId }); let attr = data?.data['custom-riffCard-State-Fake'] || ''; // 添加删除表情 const hasEmojis = attr.includes(emojis); attr = attr.replace(emojis, ''); if(!hasEmojis) attr = emojisPosition === 'prefix' ? emojis + attr : attr + emojis; requestApi('/api/attr/setBlockAttrs', { "id": blockId, "attrs": { "custom-riffCard-State-Fake": attr } }); } }, true); function getCursorElement() { const selection = window.getSelection(); if (selection.rangeCount > 0) { const range = selection.getRangeAt(0); // 获取选择范围的起始位置所在的节点 const startContainer = range.startContainer; // 如果起始位置是文本节点,返回其父元素节点 const cursorElement = startContainer.nodeType === Node.TEXT_NODE ? startContainer.parentElement : startContainer; return cursorElement; } return null; } async function requestApi(url, data, method = 'POST') { return await (await fetch(url, {method: method, body: JSON.stringify(data||{})})).json(); } })(); - 其他回帖
-
试试这个是否想要的效果。
由于 Ctrl+R 是替换文本,建议改成 Ctrl+T 或其他,可通过参数配置修改,详情见参数说明。
// 通过快捷键给块书签添加删除标签 // see https://ld246.com/article/1757393544265 (()=>{ // 按键字母,由于Ctrl+R是替换,建议改成别的比如Ctrl+T const pressLetter = 'R'; // 必须大写 // 添加的表情 const emojis = '😀'; // 表情位置,prefix 开头,suffix 结尾 const emojisPosition = 'prefix'; document.addEventListener('keydown', async (event) => { // 检查是否按下了 Ctrl+R // 这里推荐用 Ctrl + T,只需要把下行的 KeyR 改为 KeyT 即可 if ((event.ctrlKey || event.metaKey) && event.code === 'Key'+pressLetter && !event.shiftKey && !event.altKey) { const cursorElement = getCursorElement(); if(!cursorElement) return; const cursorBlock = cursorElement.closest('.protyle-wysiwyg [data-node-id][data-type]'); if(!cursorBlock) return; const blockId = cursorBlock?.dataset?.nodeId; if(!blockId) return; event.preventDefault(); event.stopPropagation(); // 获取书签 const data = await requestApi('/api/attr/getBlockAttrs', { "id": blockId }); let bookmark = data?.data?.bookmark || ''; // 添加删除表情 const hasEmojis = bookmark.includes(emojis); bookmark = bookmark.replace(emojis, ''); if(!hasEmojis) bookmark = emojisPosition === 'prefix' ? emojis + bookmark : bookmark + emojis; requestApi('/api/attr/setBlockAttrs', { "id": blockId, "attrs": { "bookmark": bookmark } }); } }, true); function getCursorElement() { const selection = window.getSelection(); if (selection.rangeCount > 0) { const range = selection.getRangeAt(0); // 获取选择范围的起始位置所在的节点 const startContainer = range.startContainer; // 如果起始位置是文本节点,返回其父元素节点 const cursorElement = startContainer.nodeType === Node.TEXT_NODE ? startContainer.parentElement : startContainer; return cursorElement; } return null; } async function requestApi(url, data, method = 'POST') { return await (await fetch(url, {method: method, body: JSON.stringify(data||{})})).json(); } })();效果
1 回复1 操作wilsons 在 2025-09-09 14:13:42 更新了该回帖 -
@wilsons 举手!w 佬,我想把这部分变更为在 自定义属性 custom-riffCard-State-Fake 中进行写入/删除,但我不会变……(只要这部分就好了)
// 获取书签 const data = await requestApi('/api/attr/getBlockAttrs', { "id": blockId }); let bookmark = data?.data?.bookmark || ''; // 添加删除表情 const hasEmojis = bookmark.includes(emojis); bookmark = bookmark.replace(emojis, ''); if(!hasEmojis) bookmark = emojisPosition === 'prefix' ? emojis + bookmark : bookmark + emojis; requestApi('/api/attr/setBlockAttrs', { "id": blockId, "attrs": { "bookmark": bookmark } });1 回复