Skip to content

块内可以增加自定义元素和自定义属性吗? #13325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wish5115 opened this issue Dec 1, 2024 · 13 comments
Closed

块内可以增加自定义元素和自定义属性吗? #13325

wish5115 opened this issue Dec 1, 2024 · 13 comments

Comments

@wish5115
Copy link

wish5115 commented Dec 1, 2024

In what scenarios do you need this feature?

image

如图,只要块内添加了自定义元素,哪怕仅仅修改了属性值,再次编辑块就会把自定义块删除,如果在可编辑div元素外则转义为普通字符,即使调用/api/block/updateBlock也无法把自定义元素或属性保存。

请问,有没有办法实现可以让自定义元素或属性不被删除或可以保存呢?

Describe the optimal solution

期望可以让自定义元素或属性不被删除或可以保存。

Describe the candidate solution

No response

Other information

No response

@88250
Copy link
Member

88250 commented Dec 1, 2024

自定义属性名需要用 custom- 前缀;不支持自定义标签,会被忽略。

@88250 88250 closed this as completed Dec 1, 2024
@wish5115
Copy link
Author

wish5115 commented Dec 1, 2024

自定义属性名需要用 custom- 前缀;不支持自定义标签,会被忽略。

收到,谢谢!

@wish5115
Copy link
Author

wish5115 commented Dec 1, 2024

自定义属性名需要用 custom- 前缀;不支持自定义标签,会被忽略。

请教下,是这样添加吗?添加后,再次编辑或调用updateBlock元素还是被删除。

<img alt="demo/出纳记账" class="emoji" custom-demodemodemo="demodemodemo" src="/emojis/demo/出纳记账.png" title="demo/出纳记账">

@Achuan-2
Copy link
Member

Achuan-2 commented Dec 1, 2024

应该不会被删除吧,我的脚注插件也对脚注引用和选中的文字加了自定义属性,是可以保留的
不过自定义的是span标签,img没试过

@88250
Copy link
Member

88250 commented Dec 1, 2024

只有块级元素支持自定义属性,行级元素不支持。

@wish5115
Copy link
Author

wish5115 commented Dec 1, 2024

只有块级元素支持自定义属性,行级元素不支持。

明白了,谢谢D大!

@wish5115
Copy link
Author

wish5115 commented Dec 1, 2024

应该不会被删除吧,我的脚注插件也对脚注引用和选中的文字加了自定义属性,是可以保留的 不过自定义的是span标签,img没试过

你的span是在哪里?块内吗?

这个演示的是span内,不行的。

20241201_182250.mp4

@Achuan-2
Copy link
Member

Achuan-2 commented Dec 1, 2024

直接改html源码是不会被保存的
不过怎么保存我也不会,我是直接抄别人的代码,发现能用,就继续用了

@wish5115
Copy link
Author

wish5115 commented Dec 1, 2024

直接改html源码是不会被保存的 不过怎么保存我也不会,我是直接抄别人的代码,发现能用,就继续用了

是的,修改后如果不需要手动修改块内容是没事的,如果修改的话非官方自带的元素和属性会被清除。

我觉得应该可以用块的自定义属性保存,然后加载时解析出来,每次修改后再重新渲染,太麻烦了,还是改用其他方案吧

@Achuan-2
Copy link
Member

Achuan-2 commented Dec 1, 2024

直接改html源码是不会被保存的 不过怎么保存我也不会,我是直接抄别人的代码,发现能用,就继续用了

是的,修改后如果不需要手动修改块内容是没事的,如果修改的话非官方自带的元素和属性会被清除。

我觉得应该可以用块的自定义属性保存,然后加载时解析出来,每次修改后再重新渲染,太麻烦了,还是改用其他方案吧

可以试试这个函数

let memoELement = document.querySelector(`span[data-id="${newBlockId}"]`);
if (memoELement) {
    memoELement.setAttribute("custom-footnote", newBlockId);
    // 保存脚注块引添加的自定义属性值
    saveViaTransaction(memoELement)
}

function saveViaTransaction(protyleElem) {
    let protyle: HTMLElement
    if (protyleElem != null) {
        protyle = protyleElem
    }
    if (protyle === null)
        protyle = document.querySelector(".card__block.fn__flex-1.protyle:not(.fn__none) .protyle-wysiwyg.protyle-wysiwyg--attr")
    if (protyle === null)
        protyle = document.querySelector('.fn__flex-1.protyle:not(.fn__none) .protyle-wysiwyg.protyle-wysiwyg--attr') //需要获取到当前正在编辑的 protyle
    let e = document.createEvent('HTMLEvents')
    e.initEvent('input', true, false)
    protyle.dispatchEvent(e)
}

@wish5115
Copy link
Author

wish5115 commented Dec 1, 2024

直接改html源码是不会被保存的 不过怎么保存我也不会,我是直接抄别人的代码,发现能用,就继续用了

是的,修改后如果不需要手动修改块内容是没事的,如果修改的话非官方自带的元素和属性会被清除。
我觉得应该可以用块的自定义属性保存,然后加载时解析出来,每次修改后再重新渲染,太麻烦了,还是改用其他方案吧

可以试试这个函数

let memoELement = document.querySelector(`span[data-id="${newBlockId}"]`);
if (memoELement) {
    memoELement.setAttribute("custom-footnote", newBlockId);
    // 保存脚注块引添加的自定义属性值
    saveViaTransaction(memoELement)
}

function saveViaTransaction(protyleElem) {
    let protyle: HTMLElement
    if (protyleElem != null) {
        protyle = protyleElem
    }
    if (protyle === null)
        protyle = document.querySelector(".card__block.fn__flex-1.protyle:not(.fn__none) .protyle-wysiwyg.protyle-wysiwyg--attr")
    if (protyle === null)
        protyle = document.querySelector('.fn__flex-1.protyle:not(.fn__none) .protyle-wysiwyg.protyle-wysiwyg--attr') //需要获取到当前正在编辑的 protyle
    let e = document.createEvent('HTMLEvents')
    e.initEvent('input', true, false)
    protyle.dispatchEvent(e)
}

👍👍👍 你这个确实可以耶,但仅限第一层的span,第二层就不行了,img也不行。第一层的话,不用saveViaTransaction这个函数也行。

image

@Achuan-2
Copy link
Member

Achuan-2 commented Dec 1, 2024

@wish5115 span不能嵌套,只能叠加自定义属性和data-type

@wish5115
Copy link
Author

wish5115 commented Dec 1, 2024

只能叠加自定义属性和data-type

感谢!原来如此!学习了!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants