gpt4 book ai didi

javascript - 如何限制粘贴在 contenteditable 区域中的文本样式?

转载 作者:行者123 更新时间:2023-12-01 15:16:59 28 4
gpt4 key购买 nike

我遇到了this Stack Overflow post在哪里讨论了我需要的确切内容:能够将文本粘贴到 contenteditable 区域,只保留一些样式。我在那里运行了代码片段,它工作正常。但是,当我在我的页面上尝试它时,所有样式都被删除了,包括我想要保留的样式,比如粗体和斜体。在比较了代码和一些实验之后,我意识到它不起作用的原因是因为我使用的是外部 CSS,而不是内联。
有什么办法可以使它与外部 CSS 一起工作?我永远不会知道用户将在该 contenteditable 中发布的文本的来源,以及样式是如何应用于它的,因此我希望解决所有可能性。
另外,有没有办法让它与拖放的文本一起工作,而不仅仅是粘贴的文本?我尝试将它正在监听的事件从“粘贴”替换为“删除”,但我收到错误 e.clipboardData is undefined

const el = document.querySelector('p');

el.addEventListener('paste', (e) => {
// Get user's pasted data
let data = e.clipboardData.getData('text/html') ||
e.clipboardData.getData('text/plain');

// Filter out everything except simple text and allowable HTML elements
let regex = /<(?!(\/\s*)?(b|i|em|strong|u)[>,\s])([^>])*>/g;
data = data.replace(regex, '');

// Insert the filtered content
document.execCommand('insertHTML', false, data);

// Prevent the standard paste behavior
e.preventDefault();
});
.editable {
width: 100%;
min-height: 20px;
font-size: 14px;
color: black;
font-family: arial;
line-height: 1.5;
border: solid 1px black;
margin-bottom: 30px;
}

.big {
font-size: 20px;
}

.red {
color: red;
}

.bold {
font-weight: bold;
}

.italic {
text-decoration: italic;
}
<p class="editable" contenteditable></p>

<p class="notEditable">
Try pasting this paragraph into the contenteditable paragraph above. This text includes <b>BOLD</b>, <i>ITALIC</i>, <s>STRIKE</s>, <u>UNDERLINE</u>, a <a href='#'>LINK</a>, and <span style="font-size:30px; color:red; font-family:Times New Roman">a few other styles.</span> All styles are inline, and it works as expected.
</p>

<p>Now, try pasting this paragraph with external styles. <span class="big">Big</span > <span class="red">red</span> <span class="bold">bold</span> <span class="italic">italic</span>. It no longer works.</p>

最佳答案

正如其他答案指出的那样,我不知道使用剪贴板从其他页面中获取 CSS 样式的任何方法。 .但是你自己可以做这样的事情:
获取 getComputedStyle (仅 CSS)所有元素过滤掉想要的样式,在此示例中为 fontStylefontWeight .然后你可以条件 if fontStyle==="italic"fontweight==="700" (粗体),textDecoration==="underline rgb(0, 0, 0)"并将这些元素包装到其 HTML 标记中。
您这样做是因为您的正则表达式函数仅针对标签,甚至不是内联 CSS 属性 font-style: italic; . Witch 是一个耻辱,它会让事情变得更容易,因为你可以阅读每个元素的 CSS 类样式并内联应用它,但是这样你需要调整它并应用 HTML 标签。

if ( style.fontStyle==="italic"){
element.innerHTML = "<i>" + element.innerHTML + "</i>";
;}

if ( style.fontWeight==="700"){
element.innerHTML = "<b>" + element.innerHTML + "</b>";
;}

if (style.textDecoration==="underline rgb(0, 0, 0)"){
element.innerHTML = "<u>" + element.innerHTML + "</u>";
;}
在下面的示例中,如果您复制 Now, try pasting this paragraph with external styles. Big red bold italic. It no longer works.你会得到粗体,下划线和斜体。您可以对其余的过滤选项执行相同的操作。

const el = document.querySelector('p');

el.addEventListener('paste', (e) => {
// Get user's pasted data
let data = e.clipboardData.getData('text/html') ||
e.clipboardData.getData('text/plain');
//console.log(data)
// Filter out everything except simple text and allowable HTML elements
let regex = /<(?!(\/\s*)?(b|i|em|strong|u)[>,\s])([^>])*>/g;
data = data.replace(regex, '');
//console.log(data)
// Insert the filtered content
document.execCommand('insertHTML', false, data);

// Prevent the standard paste behavior
e.preventDefault();
});

[...document.querySelectorAll('body *')].forEach(element=>{
const style = getComputedStyle(element)

if ( style.fontStyle==="italic"){
element.innerHTML = "<i>" + element.innerHTML + "</i>";
;}

if ( style.fontWeight==="700"){
element.innerHTML = "<b>" + element.innerHTML + "</b>";
;}

if (style.textDecoration==="underline rgb(0, 0, 0)"){
element.innerHTML = "<u>" + element.innerHTML + "</u>";
;}
});
.editable {
width: 100%;
min-height: 20px;
font-size: 14px;
color: black;
font-family: arial;
line-height: 1.5;
border: solid 1px black;
margin-bottom: 30px;
}

.big {
font-size: 20px;
}

.red {
color: red;
}

.bold {
font-weight: bold;
}
.underline{
text-decoration: underline;
}
.italic {
font-style: italic;
}
<p class="editable" contenteditable></p>

<p class="notEditable">
Try pasting this paragraph into the contenteditable paragraph above. This text includes <b>BOLD</b>, <i>ITALIC</i>, <s>STRIKE</s>, <u>UNDERLINE</u>, a <a href='#'>LINK</a>, and <span style="font-size:30px; color:red; font-family:Times New Roman">a few other styles.</span> All styles are inline, and it works as expected.
</p>

<p id="container"><span class="underline">Now</span>, try pasting this paragraph with external styles. <span class="big">Big</span > <span class="red">red</span> <span class="bold" >bold</span> <span class="italic">italic</span>. It no longer works.</p>

关于javascript - 如何限制粘贴在 contenteditable 区域中的文本样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63052383/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com