123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- // SiYuan - Refactor your thinking
- // Copyright (c) 2020-present, b3log.org
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- package model
- import (
- "errors"
- "os"
- "path"
- "path/filepath"
- "sync"
- "github.com/88250/gulu"
- "github.com/88250/lute/parse"
- "github.com/siyuan-note/filelock"
- "github.com/siyuan-note/logging"
- "github.com/siyuan-note/siyuan/kernel/treenode"
- "github.com/siyuan-note/siyuan/kernel/util"
- )
- type RecentDoc struct {
- RootID string `json:"rootID"`
- Icon string `json:"icon"`
- Title string `json:"title"`
- }
- var recentDocLock = sync.Mutex{}
- func RemoveRecentDoc(ids []string) {
- recentDocLock.Lock()
- defer recentDocLock.Unlock()
- recentDocs, err := getRecentDocs()
- if nil != err {
- return
- }
- ids = gulu.Str.RemoveDuplicatedElem(ids)
- for i, doc := range recentDocs {
- if gulu.Str.Contains(doc.RootID, ids) {
- recentDocs = append(recentDocs[:i], recentDocs[i+1:]...)
- break
- }
- }
- err = setRecentDocs(recentDocs)
- if nil != err {
- return
- }
- return
- }
- func SetRecentDocByTree(tree *parse.Tree) {
- recentDoc := &RecentDoc{
- RootID: tree.Root.ID,
- Icon: tree.Root.IALAttr("icon"),
- Title: tree.Root.IALAttr("title"),
- }
- SetRecentDoc(recentDoc)
- }
- func SetRecentDoc(doc *RecentDoc) (err error) {
- recentDocLock.Lock()
- defer recentDocLock.Unlock()
- recentDocs, err := getRecentDocs()
- if nil != err {
- return
- }
- for i, c := range recentDocs {
- if c.RootID == doc.RootID {
- recentDocs = append(recentDocs[:i], recentDocs[i+1:]...)
- break
- }
- }
- recentDocs = append([]*RecentDoc{doc}, recentDocs...)
- if 32 < len(recentDocs) {
- recentDocs = recentDocs[:32]
- }
- err = setRecentDocs(recentDocs)
- return
- }
- func GetRecentDocs() (ret []*RecentDoc, err error) {
- recentDocLock.Lock()
- defer recentDocLock.Unlock()
- return getRecentDocs()
- }
- func setRecentDocs(recentDocs []*RecentDoc) (err error) {
- dirPath := filepath.Join(util.DataDir, "storage")
- if err = os.MkdirAll(dirPath, 0755); nil != err {
- logging.LogErrorf("create storage [recent-doc] dir failed: %s", err)
- return
- }
- data, err := gulu.JSON.MarshalIndentJSON(recentDocs, "", " ")
- if nil != err {
- logging.LogErrorf("marshal storage [recent-doc] failed: %s", err)
- return
- }
- lsPath := filepath.Join(dirPath, "recent-doc.json")
- err = filelock.WriteFile(lsPath, data)
- if nil != err {
- logging.LogErrorf("write storage [recent-doc] failed: %s", err)
- return
- }
- return
- }
- func getRecentDocs() (ret []*RecentDoc, err error) {
- tmp := []*RecentDoc{}
- dataPath := filepath.Join(util.DataDir, "storage/recent-doc.json")
- if !filelock.IsExist(dataPath) {
- return
- }
- data, err := filelock.ReadFile(dataPath)
- if nil != err {
- logging.LogErrorf("read storage [recent-doc] failed: %s", err)
- return
- }
- if err = gulu.JSON.UnmarshalJSON(data, &tmp); nil != err {
- logging.LogErrorf("unmarshal storage [recent-doc] failed: %s", err)
- return
- }
- var notExists []string
- for _, doc := range tmp {
- if bt := treenode.GetBlockTree(doc.RootID); nil != bt {
- doc.Title = path.Base(bt.HPath) // Recent docs not updated after renaming https://github.com/siyuan-note/siyuan/issues/7827
- ret = append(ret, doc)
- } else {
- notExists = append(notExists, doc.RootID)
- }
- }
- if 0 < len(notExists) {
- setRecentDocs(ret)
- }
- return
- }
- type Criterion struct {
- Name string `json:"name"`
- Sort int `json:"sort"` // 0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时)
- Group int `json:"group"` // 0:不分组,1:按文档分组
- HasReplace bool `json:"hasReplace"` // 是否有替换
- Method int `json:"method"` // 0:文本,1:查询语法,2:SQL,3:正则表达式
- HPath string `json:"hPath"`
- IDPath []string `json:"idPath"`
- K string `json:"k"` // 搜索关键字
- R string `json:"r"` // 替换关键字
- Types *CriterionTypes `json:"types"` // 类型过滤选项
- ReplaceTypes *CriterionReplaceTypes `json:"replaceTypes"` // 替换类型过滤选项
- }
- type CriterionTypes struct {
- MathBlock bool `json:"mathBlock"`
- Table bool `json:"table"`
- Blockquote bool `json:"blockquote"`
- SuperBlock bool `json:"superBlock"`
- Paragraph bool `json:"paragraph"`
- Document bool `json:"document"`
- Heading bool `json:"heading"`
- List bool `json:"list"`
- ListItem bool `json:"listItem"`
- CodeBlock bool `json:"codeBlock"`
- HtmlBlock bool `json:"htmlBlock"`
- EmbedBlock bool `json:"embedBlock"`
- DatabaseBlock bool `json:"databaseBlock"`
- AudioBlock bool `json:"audioBlock"`
- VideoBlock bool `json:"videoBlock"`
- IFrameBlock bool `json:"iframeBlock"`
- WidgetBlock bool `json:"widgetBlock"`
- }
- type CriterionReplaceTypes struct {
- Text bool `json:"text"`
- ImgText bool `json:"imgText"`
- ImgTitle bool `json:"imgTitle"`
- ImgSrc bool `json:"imgSrc"`
- AText bool `json:"aText"`
- ATitle bool `json:"aTitle"`
- AHref bool `json:"aHref"`
- Code bool `json:"code"`
- Em bool `json:"em"`
- Strong bool `json:"strong"`
- InlineMath bool `json:"inlineMath"`
- InlineMemo bool `json:"inlineMemo"`
- Kbd bool `json:"kbd"`
- Mark bool `json:"mark"`
- S bool `json:"s"`
- Sub bool `json:"sub"`
- Sup bool `json:"sup"`
- Tag bool `json:"tag"`
- U bool `json:"u"`
- DocTitle bool `json:"docTitle"`
- CodeBlock bool `json:"codeBlock"`
- MathBlock bool `json:"mathBlock"`
- HtmlBlock bool `json:"htmlBlock"`
- }
- var criteriaLock = sync.Mutex{}
- func RemoveCriterion(name string) (err error) {
- criteriaLock.Lock()
- defer criteriaLock.Unlock()
- criteria, err := getCriteria()
- if nil != err {
- return
- }
- for i, c := range criteria {
- if c.Name == name {
- criteria = append(criteria[:i], criteria[i+1:]...)
- break
- }
- }
- err = setCriteria(criteria)
- return
- }
- func SetCriterion(criterion *Criterion) (err error) {
- if "" == criterion.Name {
- return errors.New(Conf.Language(142))
- }
- criteriaLock.Lock()
- defer criteriaLock.Unlock()
- criteria, err := getCriteria()
- if nil != err {
- return
- }
- update := false
- for i, c := range criteria {
- if c.Name == criterion.Name {
- criteria[i] = criterion
- update = true
- break
- }
- }
- if !update {
- criteria = append(criteria, criterion)
- }
- err = setCriteria(criteria)
- return
- }
- func GetCriteria() (ret []*Criterion) {
- criteriaLock.Lock()
- defer criteriaLock.Unlock()
- ret, _ = getCriteria()
- return
- }
- func setCriteria(criteria []*Criterion) (err error) {
- dirPath := filepath.Join(util.DataDir, "storage")
- if err = os.MkdirAll(dirPath, 0755); nil != err {
- logging.LogErrorf("create storage [criteria] dir failed: %s", err)
- return
- }
- data, err := gulu.JSON.MarshalIndentJSON(criteria, "", " ")
- if nil != err {
- logging.LogErrorf("marshal storage [criteria] failed: %s", err)
- return
- }
- lsPath := filepath.Join(dirPath, "criteria.json")
- err = filelock.WriteFile(lsPath, data)
- if nil != err {
- logging.LogErrorf("write storage [criteria] failed: %s", err)
- return
- }
- return
- }
- func getCriteria() (ret []*Criterion, err error) {
- ret = []*Criterion{}
- dataPath := filepath.Join(util.DataDir, "storage/criteria.json")
- if !filelock.IsExist(dataPath) {
- return
- }
- data, err := filelock.ReadFile(dataPath)
- if nil != err {
- logging.LogErrorf("read storage [criteria] failed: %s", err)
- return
- }
- if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
- logging.LogErrorf("unmarshal storage [criteria] failed: %s", err)
- return
- }
- return
- }
- var localStorageLock = sync.Mutex{}
- func RemoveLocalStorageVals(keys []string) (err error) {
- localStorageLock.Lock()
- defer localStorageLock.Unlock()
- localStorage := getLocalStorage()
- for _, key := range keys {
- delete(localStorage, key)
- }
- return setLocalStorage(localStorage)
- }
- func SetLocalStorageVal(key string, val interface{}) (err error) {
- localStorageLock.Lock()
- defer localStorageLock.Unlock()
- localStorage := getLocalStorage()
- localStorage[key] = val
- return setLocalStorage(localStorage)
- }
- func SetLocalStorage(val interface{}) (err error) {
- localStorageLock.Lock()
- defer localStorageLock.Unlock()
- return setLocalStorage(val)
- }
- func GetLocalStorage() (ret map[string]interface{}) {
- localStorageLock.Lock()
- defer localStorageLock.Unlock()
- return getLocalStorage()
- }
- func setLocalStorage(val interface{}) (err error) {
- if util.ReadOnly {
- return
- }
- dirPath := filepath.Join(util.DataDir, "storage")
- if err = os.MkdirAll(dirPath, 0755); nil != err {
- logging.LogErrorf("create storage [local] dir failed: %s", err)
- return
- }
- data, err := gulu.JSON.MarshalIndentJSON(val, "", " ")
- if nil != err {
- logging.LogErrorf("marshal storage [local] failed: %s", err)
- return
- }
- lsPath := filepath.Join(dirPath, "local.json")
- err = filelock.WriteFile(lsPath, data)
- if nil != err {
- logging.LogErrorf("write storage [local] failed: %s", err)
- return
- }
- return
- }
- func getLocalStorage() (ret map[string]interface{}) {
- // When local.json is corrupted, clear the file to avoid being unable to enter the main interface https://github.com/siyuan-note/siyuan/issues/7911
- ret = map[string]interface{}{}
- lsPath := filepath.Join(util.DataDir, "storage/local.json")
- if !filelock.IsExist(lsPath) {
- return
- }
- data, err := filelock.ReadFile(lsPath)
- if nil != err {
- logging.LogErrorf("read storage [local] failed: %s", err)
- return
- }
- if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
- logging.LogErrorf("unmarshal storage [local] failed: %s", err)
- return
- }
- return
- }
|