可以将jQuery脚本添加到dataviewjs片段中以更改进度条外观吗?

zpgglvta  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(139)

我在一个markdown文件中运行了一个dataviewjs片段,特别是Obsidian,但我对进度条的外观不满意,它使用绿色表示“刚刚开始或不好”,红色表示“完成或良好”。我想扭转这种局面。我发现了一个jQuery脚本,我认为它会有帮助,但我不能让它工作(极端js菜鸟,在这里)。
下面是dataviewjs代码片段:

const createProgressBar = (char_count, char_goal, words_count, words_goal) => {
        let percents = 0
        if (char_goal != undefined) {
            percents = Math.round(char_count / char_goal * 100)
            else if (words_goal != undefined) {
                percents = Math.round(words_count / words_goal * 100)
            }
            dv.paragraph("<progress max=100 value=" + percents + "> </progress> " + percents + "%")
        }

        const getFirstSplit = (split) => {
                if (split.type == "tabs") {
                    return split
                    else if (split.type == "split") {
                        return getFirstSplit(split.children[0])
                    }
                }

                let firstSplit = getFirstSplit(app.workspace.rootSplit)

                let current = firstSplit.children[firstSplit.currentTab].view.file

                if (current != null) {
                    let page = dv.page(current.path)

                    if (page != undefined) {
                        let char_goal = page.char_goal
                        let words_goal = page.words_goal
                        let synopsis = page.synopsis
                        let POV = page.pov
                        let location = page.location
                        let characters = page.characters
                        let tags = page.tags
                        let project = page.project
                        let highlights

                        if (project != undefined) {
                            dv.header(4, "[[" + project + "]]")
                        }

                        if (synopsis != undefined) {
                            dv.span("***")
                            dv.header(5, "Synopsis")
                            dv.span(synopsis)
                        }

                        if (POV != undefined) {
                            dv.span("***")
                            dv.header(5, "POV")
                            dv.span(POV)
                        }

                        if (page.type == "writing scene") {

                            let content = await dv.io.load(current.path)
                            highlights = content.match(/==.*?==/g)
                            content = content.replace(/^---\n.*?\n---/ms, "").trim().replaceAll("—", "").replaceAll(/[\n]+/mg, " ").replaceAll(/[ ]+/mg, " ").replaceAll("==", "").replaceAll("*", "").replaceAll("#", "")
                            let words = content.split(" ")

                            dv.span("***")
                            dv.header(5, "Scene progress")

                            if (words_goal != undefined) {
                                dv.paragraph(words.length + " / " + words_goal + " words")
                            }

                            if (char_goal != undefined) {
                                dv.paragraph(content.length + " / " + char_goal + " characters")
                            }

                            createProgressBar(content.length, char_goal, words.length, words_goal)
                        }



                        if (page.type == "writing project") {
                            let path = '"' + page.file.folder + '"'
                            let scenePages = dv.pages(path).filter(p => p.type == "writing scene")
                            let words_count = 0
                            let char_count = 0

                            let scenesData = []

                            for (let scene of scenePages) {
                                let sceneContent = await dv.io.load(scene.file.path)
                                sceneContent = sceneContent.replace(/^---\n.*?\n---/ms, "").trim().replaceAll("—", "").replaceAll(/[\n]+/mg, " ").replaceAll(/[ ]+/mg, " ").replaceAll("==", "").replaceAll("*", "").replaceAll("#", "")
                                let sceneWords = sceneContent.split(" ")
                                words_count = words_count + sceneWords.length
                                char_count = char_count + sceneContent.length
                                scenesData.push({
                                    words_count: sceneWords.length,
                                    char_count: sceneContent.length,
                                    link: scene.file.link,
                                    words_goal: scene.words_goal,
                                    char_goal: scene.char_goal
                                })
                            }

                            dv.span("***")
                            dv.header(5, "Novel progress")

                            if (words_goal != undefined) {
                                dv.paragraph(words_count + " / " + words_goal + " words")
                            }
                            if (char_goal != undefined) {
                                dv.paragraph(char_count + " / " + char_goal + " characters")
                            }
                            createProgressBar(char_count, char_goal, words_count, words_goal)

                            dv.span("***")
                            dv.header(5, "Scenes progress")

                            scenesData.forEach(scene => {
                                dv.span(scene.link)
                                createProgressBar(scene.char_count, scene.char_goal, scene.words_count, scene.words_goal)
                            })
                        }


                        if (location != undefined) {
                            dv.span("***")
                            dv.header(5, "Location")
                            dv.span(location.map(c => " [[" + c + "]]") + "")
                        }

                        if (characters != undefined) {
                            dv.span("***")
                            dv.header(5, "Characters")
                            dv.span(characters.map(c => " [[" + c + "]]") + "")
                        }

                        if (tags != undefined) {
                            dv.span("***")
                            dv.header(5, "Tags")
                            dv.span(tags.map(c => " #" + c) + "")
                        }

                        if (highlights != undefined) {
                            highlights = highlights.map(h => h.replaceAll("==", ""))
                            dv.span("***")
                            dv.header(5, "Highlights")
                            dv.paragraph(highlights)
                        }
                    }
                }

字符串
下面是jQuery脚本:

$(document).ready(function() {
            const show_percent = true;
            var progressBars = $(".progress-bar");
            for (i = 0; i < progressBars.length; i++) {
                var progress = $(progressBars[i]).attr("aria-valuenow");
                $(progressBars[i]).width(progress + "%");
                if (show_percent) {
                    $(progressBars[i]).text(progress + "%");
                }
                if (progress >= "90") {
                    //90 and above
                    $(progressBars[i]).addClass("bg-success");
                    else if (progress >= "30" && progress < "45") {
                        $(progressBars[i]).addClass("bg-warning"); //From 30 to 44
                        else if (progress >= "45" && progress < "90") {
                            $(progressBars[i]).addClass("bg-info"); //From 45 to 89
                            else {
                                //29 and under
                                $(progressBars[i]).addClass("bg-danger");
                            }
                        }
                    });


是否可以将jquery添加到dataview片段中,如果可以,在哪里以及如何执行?
我尝试使用jQuery脚本,更改属性以匹配dataviewjs参数,但我得到以下错误消息:

Evaluation Error: TypeError: Cannot set properties of undefined (setting '__k')
    at S$1 (plugin:dataview:14624:8423)
    at q$1 (plugin:dataview:14624:8598)
    at $ (plugin:dataview:14628:5017)
    at eval (eval at <anonymous> (plugin:dataview), <anonymous>:1:39)
    at DataviewInlineApi.eval (plugin:dataview:18404:16)
    at evalInContext (plugin:dataview:18405:7)
    at asyncEvalInContext (plugin:dataview:18415:32)
    at DataviewJSRenderer.render (plugin:dataview:18436:19)
    at DataviewJSRenderer.onload (plugin:dataview:18020:14)
    at e.load (app://obsidian.md/app.js:1:631581)

ubby3x7f

ubby3x7f1#

我不认为jQuery在Obsidian中得到支持。
作为替代方案,您可以add the following CSS snippet

.progress-red::-webkit-progress-value {
  background-color: red !important;
}

.progress-orange::-webkit-progress-value {
    background-color: orange !important;
  }

.progress-greenyellow::-webkit-progress-value {
background-color: greenyellow !important;
}

字符串
并调整你的JS createProgressBar如下

const createProgressBar = (char_count, char_goal, words_count, words_goal) => {
    let percents = 0
    if (char_goal != undefined) {
        percents = Math.round(char_count / char_goal * 100)
    }
    else if (words_goal != undefined) {
        percents = Math.round(words_count / words_goal * 100)
    }
    let color='red';
    if(percents >=75)

        color = 'greenyellow';
    else if(percents >=50)
            color = 'orange';

    dv.paragraph(`<progress class="progress-${color}" max=100 value=${percents}></progress> ${percents}% `)
}

createProgressBar(12, undefined, 30.8, 100)
createProgressBar(400, 500, 30.8, 100)
createProgressBar(400, undefined, 234, 423)

相关问题