Skip to content

Commit

Permalink
Merge pull request #111 from Higurashi-kagome/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Higurashi-kagome authored Oct 3, 2023
2 parents 003743a + 1d6a534 commit 19e2e78
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
"**/dist": true
},
"editor.detectIndentation": false, // 不自动检测缩进
"editor.tabSize": 4,
"editor.tabSize": 4
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h2 align="center"><img src="res/README/icon128.png" height="128"><br>Wereader</h2>
<h2 align="center"><img src="res/README/icon128.png" height="100" alt="icon"><br>Wereader</h2>

<p align="center"><strong>一个 Chrome / Firefox 扩展:主要用于微信读书做笔记,对常使用 Markdown 做笔记的读者比较有帮助。</strong></p>

Expand Down Expand Up @@ -53,3 +53,12 @@
| [chrome-plugin-demo](https://github.com/sxei/chrome-plugin-demo) | Chrome 扩展开发教程 |
| [Extensions - Chrome Developers](https://developer.chrome.com/docs/extensions/) | Chrome 扩展官方文档 |

## 其他

<table>
<tr>
<td>
<div align="center"><img src="res/README/wx-code.png" height="260" alt="wx-code"><br><strong>加微信进交流群(备注:wereader 交流群)</strong></div>
</td>
</tr>
</table>
Binary file added res/README/wx-code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ export function commandCopy(text: string, selector: string) {
textEl.select()
document.execCommand('copy')
}

/**
* 时间戳转时间字符串
* @param timestamp 时间戳(秒为单位)
* @returns 格式化时间字符串
*/
export function formatTimestamp(timestamp: number) {
// 乘 1000 转为毫秒单位
const date = new Date(timestamp * 1000)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
const seconds = String(date.getSeconds()).padStart(2, '0')

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
2 changes: 2 additions & 0 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { initTheme } from './content/modules/content-theme'
import { initThoughtEdit } from './content/modules/content-thought-edit'
import { initFancyBox } from './content/modules/fancybox'
import './content/modules/content-copy'
import './content/static/css/content-theme-switch.css'
import './content/static/css/common.css'

initAlert()
initDeleteMarksNotification()
Expand Down
43 changes: 38 additions & 5 deletions src/content/modules/content-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,18 @@ function changeTheme(currentFlag: number, event?: JQuery.ClickEvent) {
* 添加主题切换按钮并绑定点击事件
*/
function addThemeBtn() {
// 主题切换顺序
const themeOrder = [-1, 0, 1, 2]
// 创建主题切换按钮
const themeBtn = $('<button title=\'主题\' class=\'readerControls_item theme\'></button>')
themeBtn.append($('<span>主题</span>'))
themeBtn.attr('z-index', 100)
// 隐藏原主题切换按钮图标
$('.readerControls_item.white > .icon,.readerControls_item.dark > .icon').css('display', 'none').before(themeBtn)
// 主题切换按钮点击事件
themeBtn.on('click', (event) => {
themeBtn.on('click', function onClick(event) {
try {
curFlag = themeOrder[(themeOrder.indexOf(curFlag) + 1) % themeOrder.length]
changeTheme(curFlag, event)
$('.readerControls_item.theme').css('display', 'none')
$('.theme_switcher').css('display', 'flex')
event.stopPropagation()
} catch (error) {
Swal.fire({
title: 'Oops...', text: '似乎出了点问题,刷新一下试试吧~', icon: 'error', confirmButtonText: 'OK'
Expand All @@ -111,6 +110,39 @@ function addThemeBtn() {
})
}

/**
* 添加主题切换
*/
function addThemeSwitcher() {
$('.readerControls_item.white > .icon,.readerControls_item.dark > .icon').before($(`<div class="readerControls_fontSize expand theme_switcher" style="
width: 160px; display: none; margin-top: 0;">
<div class="fontSizeLabel left theme-switch-left"><span>主题</span></div>
<div class="fontSizeSliderContainer theme-switch-container cursor-default">
<span style="background: #E8DDC2;" data-theme-index="1"></span>
<span style="background: #bee5c5;" data-theme-index="0"></span>
<span style="background: black;" data-theme-index="2"></span>
<span style="background: white;" data-theme-index="-1"></span></div></div>`))
// 不同主题按钮
const itemClass = 'theme-switch-item'
$('.theme-switch-container span').addClass([itemClass, 'cursor-pointer'])
.on('click', function onClick(e) {
const themeIndex = $(this).data('themeIndex')
changeTheme(themeIndex, e)
})
// 点击父元素时关闭冒泡
$('.theme-switch-container,.theme-switch-left').on('click', function handleClick(e) {
if (!$(e.target).is(`.${itemClass}`)) e.stopPropagation()
})
// 隐藏面板
$(document).add('.theme-switch-left').on('click', function handleHideSwitcher(e) {
// 不是点击切换选项时关闭切换面板
if (!$(e.target).is(`.${itemClass}`)) {
$('.theme_switcher').css('display', 'none')
$('.readerControls_item.theme').css('display', 'inline-block')
}
})
}

function initTheme() {
console.log(tag, 'initTheme')
// 主题初始化(记住上次设置的背景主题)
Expand Down Expand Up @@ -141,6 +173,7 @@ function initTheme() {
})
observer.observe(themeSwitch, { attributes: true })
addThemeBtn()
addThemeSwitcher()
}
})
}
Expand Down
6 changes: 6 additions & 0 deletions src/content/static/css/common.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.cursor-pointer {
cursor: pointer;
}
.cursor-default {
cursor: default;
}
7 changes: 7 additions & 0 deletions src/content/static/css/content-theme-switch.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* 主题面板项 */
.theme-switch-container span{
border: 2px solid grey;
border-radius: 3px;
padding: 6px;
margin: 3px;
}
4 changes: 2 additions & 2 deletions src/content/static/css/theme/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
}

/* 主题按钮、进度按钮 */
.readerControls_item.theme, #progressText {
.readerControls_item.theme, .theme-switch-left, #progressText {
color: rgb(190,190,190) !important;
}
}
4 changes: 2 additions & 2 deletions src/content/static/css/theme/white.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
}

/* 主题按钮、进度按钮 */
.readerControls_item.theme, #progressText {
.readerControls_item.theme, .theme-switch-left, #progressText {
color: rgb(0,0,0) !important;
}
}
6 changes: 3 additions & 3 deletions src/options/options-help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const helpContent: {[key: string]: any} = {
},
thouSuf: {
title: '想法后缀',
help: `<p><span class="lineinline">想法</span>也就是你平时阅读时在书中发表的想法。当你将前后缀设置为两个星号(**)时,导出的想法在 Markdown 中将会被加粗。</p>`
help: `<p><span class="lineinline">想法</span>也就是你平时阅读时在书中发表的想法。当你将前后缀设置为两个星号(**)时,导出的想法在 Markdown 中将会被加粗。可使用<span class="lineinline">{createTime}</span>作为占位符导出想法创建时间。</p>`
},
metaTemplate: {
title: '书本信息导出模板',
Expand All @@ -50,7 +50,7 @@ const helpContent: {[key: string]: any} = {
},
thouMarkSuf: {
title: '想法标注后缀',
help: `<p><span class="lineinline">想法标注</span>即想法所对应的书本内容。当你将想法标注前后缀设置为两个星号(**)时,导出的想法标注在 Markdown 中将会被加粗。</p>`
help: `<p><span class="lineinline">想法标注</span>即想法所对应的书本内容。当你将想法标注前后缀设置为两个星号(**)时,导出的想法标注在 Markdown 中将会被加粗。可使用<span class="lineinline">{createTime}</span>作为占位符导出想法创建时间。</p>`
},
codeSuf: {
title: '代码块后缀',
Expand Down Expand Up @@ -190,4 +190,4 @@ function initHelpContent() {
});
}

export { initHelpContent };
export { initHelpContent };
1 change: 1 addition & 0 deletions src/worker/types/ThoughtsInAChap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface ThoughtsInAChap {
range: string;
abstract: string;
content: string;
createTime: number
}
42 changes: 38 additions & 4 deletions src/worker/worker-popup-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {
} from './worker-vars'
import { Wereader } from './types/Wereader'
import { ThoughtsInAChap } from './types/ThoughtsInAChap'
import { getLocalStorage, getSyncStorage, sortByKey } from '../common/utils'
import {
formatTimestamp, getLocalStorage, getSyncStorage, sortByKey
} from '../common/utils'
import { notify } from './worker-notification'

// 给标题添加前后缀
Expand Down Expand Up @@ -77,7 +79,12 @@ export async function getMyThought() {
} else {
range = range.replace(/(\d*)-\d*/, '$1')
}
thoughtsInAChap.push({ abstract: abstract, content: content, range: range })
thoughtsInAChap.push({
abstract,
content,
range,
createTime: item.review.createTime
})
}
}
thoughtsMap.set(chapterUid, sortByKey(thoughtsInAChap, 'range') as ThoughtsInAChap[])
Expand Down Expand Up @@ -369,6 +376,27 @@ function addMarkPreAndSuf(markText: string, style: number, config: ConfigType) {
return pre + markText + suf
}

/**
* 替换想法前后缀配置中的占位符
* @param mark 想法标注
* @param config 配置信息
* @returns 替换后的各前后缀
*/
export function getReplacedThoughtConfig(mark: ThoughtsInAChap, config: ConfigType) {
const re = /\{createTime}/g
const time = formatTimestamp(mark.createTime)
const thouPre = config.thouPre.replace(re, time)
const thouSuf = config.thouSuf.replace(re, time)
const thouMarkPre = config.thouMarkPre.replace(re, time)
const thouMarkSuf = config.thouMarkSuf.replace(re, time)
return {
thouPre,
thouSuf,
thouMarkPre,
thouMarkSuf
}
}

// 处理章内标注
export function traverseMarks(
marks: (Updated | ThoughtsInAChap)[],
Expand All @@ -395,11 +423,17 @@ export function traverseMarks(
footnoteContent = data[1]
}
if (isThought(mark)) { // 如果为想法
const {
thouPre,
thouSuf,
thouMarkPre,
thouMarkSuf
} = getReplacedThoughtConfig(mark, config)
// 想法
const thouContent = `${config.thouPre}${mark.content}${config.thouSuf}\n\n`
const thouContent = `${thouPre}${mark.content}${thouSuf}\n\n`
// 想法所标注的内容
const abstract = mark.abstract
let thouAbstract = `${config.thouMarkPre}${abstract}${config.thouMarkSuf}\n\n`
let thouAbstract = `${thouMarkPre}${abstract}${thouMarkSuf}\n\n`
// 想法所对应文本与上一条标注相同时
if (abstract === prevMarkText) {
if (prevMarkType === '0') {
Expand Down
12 changes: 9 additions & 3 deletions src/worker/worker-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getBestBookMarks,
getBookMarks,
getChapters,
getMyThought,
getMyThought, getReplacedThoughtConfig,
getTitleAddedPreAndSuf,
traverseMarks
} from './worker-popup-process'
Expand Down Expand Up @@ -224,10 +224,16 @@ export async function copyThought(isAll?: boolean) {
let tempRes = `${getTitleAddedPreAndSuf(contents.get(chapUid)!.title, contents.get(chapUid)!.level, config)}\n\n`
let prevAbstract = '' // 保存上一条想法对应标注文本
thoughtsInAChap.forEach((thou) => {
const {
thouPre,
thouSuf,
thouMarkPre,
thouMarkSuf
} = getReplacedThoughtConfig(thou, config)
// 想法
const thouContent = `${config.thouPre}${thou.content}${config.thouSuf}\n\n`
const thouContent = `${thouPre}${thou.content}${thouSuf}\n\n`
// 想法所标注的内容
let thouAbstract = `${config.thouMarkPre}${thou.abstract}${config.thouMarkSuf}\n\n`
let thouAbstract = `${thouMarkPre}${thou.abstract}${thouMarkSuf}\n\n`
// 当前标注文本和前一条标注文本内容相同、且配置去重时,不导出当前的标注
if (thou.abstract === prevAbstract && config.distinctThouMarks) {
thouAbstract = ''
Expand Down
2 changes: 1 addition & 1 deletion src/worker/worker-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const defaultConfig: ConfigType = {
lev2Suf: '',
lev3Pre: '#### ',
lev3Suf: '',
thouPre: '==',
thouPre: '> {createTime}\n\n==',
thouSuf: '==',
/* eslint-disable */
metaTemplate: '__metaTemplate__',
Expand Down
Binary file modified wereader.zip
Binary file not shown.

0 comments on commit 19e2e78

Please sign in to comment.