Skip to content

Commit

Permalink
[feature] grep
Browse files Browse the repository at this point in the history
  • Loading branch information
WindLX committed Dec 10, 2023
1 parent cd5bbda commit 1ce04c8
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 104 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 简介

Pap 是一个pdf文件管理软件,提供了简洁的世界 Web 页面,通过标签对文件进行管理,可为托管文件自由添加 markdown 笔记.
Pap 是一个 markdown 笔记软件,通过标签对笔记进行管理,自动生成引用关系网络.

## 安装

Expand Down Expand Up @@ -40,4 +40,4 @@ Pap 需要 Python 3.11.4 的运行环境. 所有脚本均可通过 `Get-Help scr
- [x] Resource
- [x] Pdf viewer
- [x] Data Export
- [ ] Table
- [x] Grep
4 changes: 1 addition & 3 deletions md/md_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ edition = "2021"

[features]
default = ["pap"]
pap = ["math", "slide", "table", "extra"]
pap = ["math", "extra"]
extra = []
math = []
table = []
slide = []
parallel = ["rayon"]

[dependencies]
Expand Down
6 changes: 0 additions & 6 deletions md/md_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub enum SplitBlock {
Line,
#[cfg(feature = "math")]
MathBlock(MdChars),
#[cfg(feature = "extra")]
TableBlock(MdChars),
}

impl ToString for SplitBlock {
Expand All @@ -25,8 +23,6 @@ impl ToString for SplitBlock {
SplitBlock::Line => "---".to_string(),
#[cfg(feature = "math")]
SplitBlock::MathBlock(s) => format!("$$\n{}\n$$", s.to_string()),
#[cfg(feature = "extra")]
SplitBlock::TableBlock(s) => format!(":::table\n{}\n:::", s.to_string()),
}
}
}
Expand All @@ -42,8 +38,6 @@ pub enum Block<'md> {
Line,
#[cfg(feature = "math")]
MathBlock(&'md str),
#[cfg(feature = "table")]
TableBlock,
#[cfg(feature = "extra")]
TodoItem(TodoItem<'md>),
#[cfg(feature = "extra")]
Expand Down
10 changes: 0 additions & 10 deletions md/md_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ impl<'md> Parser {
SplitBlock::MathBlock(mb) => {
Ok(Block::MathBlock(<MdChars as Into<&str>>::into(mb.clone())))
}
#[cfg(feature = "table")]
SplitBlock::TableBlock(tb) => {
self.parse_table(<MdChars as Into<&str>>::into(tb.clone()))
}
}
}

Expand Down Expand Up @@ -487,12 +483,6 @@ impl<'md> Parser {
return Err(MdError::FooterError(String::from("expect ']: '")));
}
}

#[cfg(feature = "table")]
fn parse_table(&mut self, _input: &'md str) -> Result<Block<'md>> {
// TODO
Ok(Block::TableBlock)
}
}

pub fn parse(sb: &SplitBlock) -> Result<Block> {
Expand Down
20 changes: 0 additions & 20 deletions md/md_parser/src/spliter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ pub fn split(input: &str) -> Vec<SplitBlock> {
continue;
}
}
#[cfg(feature = "table")]
{
if input.check_str(":::table") {
for _ in 0..9 {
input.next();
}
let table = input.slice_to_str(":::");
blocks.push(SplitBlock::TableBlock(MdChars::new(table)));
continue;
}
}
if input.check_str("```") {
input.next();
input.next();
Expand All @@ -50,7 +39,6 @@ pub fn split(input: &str) -> Vec<SplitBlock> {
let para = input.slice_to_byte(b'\n');
if !matches!(blocks.last(), Some(SplitBlock::CodeBlock(_, _)))
&& !matches!(blocks.last(), Some(SplitBlock::MathBlock(_)))
&& !matches!(blocks.last(), Some(SplitBlock::TableBlock(_)))
{
blocks.push(SplitBlock::Paragraph(MdChars::new(para)));
}
Expand All @@ -73,14 +61,6 @@ mod tests {
dbg!(result);
}

#[test]
fn test_table() {
let input =
":::table\n| 表头1 | 表头2 |\n| ----- | ----- |\n| 内容1 | 内容2 |\n| 内容3 | 内容4 |:::";
let result = split(&input);
dbg!(result);
}

#[test]
fn test_short() {
let input = "";
Expand Down
2 changes: 0 additions & 2 deletions md/md_wasm/src/js_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub enum JsBlock {
Image(JsImage),
Line,
MathBlock(String),
TableBlock,
TodoItem(JsTodoItem),
Footer(JsFooter),
}
Expand Down Expand Up @@ -134,7 +133,6 @@ impl ToString for JsBlock {
}
}
JsBlock::Line => String::from("---"),
JsBlock::TableBlock => String::from(":::table\n:::\n"),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion md/md_wasm/src/js_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ impl SingleGenerator<Result<JsBlock, JsError>> for JsGenerator {
Block::Footer(footer) => JsBlock::Footer(self.generate_footer(footer)),
Block::ListItem(list_item) => JsBlock::ListItem(self.generate_list_item(list_item)),
Block::TodoItem(todo_item) => JsBlock::TodoItem(self.generate_todo_item(todo_item)),
Block::TableBlock => JsBlock::TableBlock,
},
Err(err) => return Err(JsError::new(&err.to_string())),
};
Expand Down
Binary file modified md_net.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pap",
"private": true,
"version": "0.1.8",
"version": "0.1.9",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
74 changes: 50 additions & 24 deletions src/components/Markdown/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,64 @@ import { nextTick, onMounted, ref } from "vue";
import { ElScrollbar } from "element-plus";
import MdBlock from "./MdBlock.vue";
const props = defineProps<{
mdData: string,
}>();
const emits = defineEmits<{
(e: 'update:mdData', value: string): void,
(e: 'onEdit'): void,
(e: 'tag'): void,
(e: 'lock'): void,
(e: 'export'): void,
(e: 'pdf'): void,
(e: 'outline'): void,
(e: 'keyboard', key: string): void
}>();
defineExpose({
saveData: () => {
const newData = saveData()
emits('update:mdData', newData)
},
grep: (regexPattern: string) => {
if (regexPattern != '') {
try {
const regex = new RegExp(regexPattern, 'g');
const matchingIndexes: number[] = [];
rawDataSet.value.forEach((str, index) => {
const match = str.match(regex);
if (match && match.length > 0) {
matchingIndexes.push(index);
}
});
grepIndex.value = matchingIndexes
} catch (e) {
}
} else {
grepIndex.value = []
highlightIndex.value = null
}
},
focusLast: () => {
if (grepIndex.value.length > 0) {
if (highlightIndex.value === null) {
highlightIndex.value = 0
} else if (highlightIndex.value > 0) {
highlightIndex.value -= 1
}
document.getElementById(`md-block-${grepIndex.value[highlightIndex.value]}`)?.scrollIntoView({ behavior: "smooth" })
}
},
focusNext: () => {
if (grepIndex.value.length > 0) {
if (highlightIndex.value === null) {
highlightIndex.value = 0
} else if (highlightIndex.value < grepIndex.value.length) {
highlightIndex.value += 1
}
document.getElementById(`md-block-${grepIndex.value[highlightIndex.value]}`)?.scrollIntoView({ behavior: "smooth" })
}
},
grepClear: () => {
grepIndex.value = []
highlightIndex.value = null
}
})
const generator = JsGenerator.new();
Expand All @@ -34,31 +72,19 @@ const blocks = ref<Array<InstanceType<typeof MdBlock>>>([]);
// data
let rawDataSet = ref<Array<string>>([""])
let grepIndex = ref<Array<number>>([])
let highlightIndex = ref<number | null>(null)
function handleKeyDown(event: KeyboardEvent) {
if (event.ctrlKey) {
event.preventDefault();
switch (event.key) {
case '1':
emits('tag')
break;
case '2':
emits('lock')
break;
case '3':
const newData = saveData();
emits('update:mdData', newData);
break;
case '4':
emits('export')
break;
case '5':
emits('pdf')
break;
case '6':
emits('outline')
break;
default:
emits('keyboard', event.key)
break;
}
}
Expand Down Expand Up @@ -132,9 +158,9 @@ onMounted(async () => {
<el-scrollbar>
<div class="editor-content" ref="container" @keydown="handleKeyDown" id="md-print">
<MdBlock v-for="(rawData, index) in rawDataSet" :key="index" :raw-data="rawData" :line-num="index"
ref="blocks" @update:raw-data="(newValue: string) => updateRawData(index, newValue)"
@append-line="appendLine" @delete-line="deleteLine" @combine-line="combineLine" @up-line="upLine"
@down-line="downLine">
:is-highlight="grepIndex.includes(index)" ref="blocks"
@update:raw-data="(newValue: string) => updateRawData(index, newValue)" @append-line="appendLine"
@delete-line="deleteLine" @combine-line="combineLine" @up-line="upLine" @down-line="downLine">
</MdBlock>
</div>
</el-scrollbar>
Expand Down
70 changes: 70 additions & 0 deletions src/components/Markdown/Grep.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<script setup lang="ts">
import { ref, computed } from 'vue';
import { ElInput } from 'element-plus'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
const emits = defineEmits<{
(e: 'grep', value: string): void
(e: 'focusLast'): void
(e: 'focusNext'): void
(e: 'clear'): void
}>()
let regex = ref('')
const errRegex = computed(() => {
try {
new RegExp(regex.value)
return false
} catch (e) {
return true
}
})
function handleLast() {
emits('focusLast')
}
function handleNext() {
emits('focusNext')
}
</script>

<template>
<div class="grep">
<el-input v-model="regex" @input="emits('grep', regex)" placeholder="查找" clearable @clear="emits('clear')"
:class="{ 'err-regex': errRegex }" />
<font-awesome-icon :icon="['fas', 'arrow-up']" class="icon" @mousedown="handleLast()" />
<font-awesome-icon :icon="['fas', 'arrow-down']" class="icon" @mousedown="handleNext()" />
</div>
</template>

<style scoped>
.grep {
position: absolute;
right: 0;
z-index: 99;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
padding: 5px 8px;
display: flex;
align-items: center;
}
.grep .icon {
margin-left: 8px;
font-size: 16px;
color: #aaaaaa;
cursor: pointer;
}
.grep .icon:hover {
color: var(--el-color-primary);
}
</style>
<style>
.err-regex .el-input__wrapper input {
color: #f56c6c;
font-weight: bold;
}
</style>
Loading

0 comments on commit 1ce04c8

Please sign in to comment.