小程序
小游戏
企业微信
微信支付
扫描小程序码分享
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
insertLink() { const that = this wx.showModal({ title: '插入链接', editable: true, placeholderText: '输入链接地址(包含http://)', success(urlRes) { if (urlRes.confirm) { wx.showModal({ title: '链接文字', editable: true, placeholderText: '输入显示文字', success(textRes) { if (textRes.confirm) { // 插入定位标识 that.editorCtx.insertText({ text: '#-*=*-*=*-*=*@-link超链接标识link-@*=*-*=*-*=*-#', success() { // 获取全文内容 that.editorCtx.getContents({ success(res) { const ops = res.delta.ops const findex = ops.findIndex(item => { return item.insert && item.insert.includes('link超链接标识link') }) if (findex > -1) { const original = ops[findex].insert const attrs = ops[findex].attributes || {} const [prefix, suffix] = original.split('#-*=*-*=*-*=*@-link超链接标识link-@*=*-*=*-*=*-#') // 构建新操作 const newOps = [] if (prefix) newOps.push({ insert: prefix, attributes: attrs }) newOps.push({ insert: textRes.content, attributes: { link: urlRes.content, color: '#007AFF', textDecoration: 'underline' } }) if (suffix) newOps.push({ insert: suffix, attributes: attrs }) // 更新编辑器内容 ops.splice(findex, 1, ...newOps) that.editorCtx.setContents({ delta: { ops }, success() { that.editorCtx.blur() } }) } } }) } }) } } }) } } }) },
这个是添加链接的方法,你试一试 看看能不能满足需要
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
function handleHtmlWithVideo(html) {
// 正则表达式用于匹配img标签中带有alt属性且alt属性值为视频链接的模式
const regex = /<img\s+src="[^"]*"\s+alt="([^"]*)"[^>]*>/g
// 使用replace方法和一个函数回调来替换匹配到的内容
return html.replace(regex, (match, videoUrl) => {
// 替换为video标签,并添加controls属性以便用户可以控制播放
return `<video width="80%" controls><source src="${videoUrl}" type="video/mp4"></video>`
})
}
我通过insertImage接口实现了插入图片、超链接、表格、公式的功能。
是的,所有上述我都通过svg图片渲染出来,通过定义data-custom数据结构保存相关属性,在里面定义type字段来区分不同元素。理论上视频也可以通过这种方式实现,渲染一个封面图或者svg图片,点击可以打开一个弹窗,播放或者修改视频。不过最终生成的html需要再额外处理一下,把里面的img标签通过data-custom字段来转换回真实的标签类型。
svg图片可以通过 src: `data:image/svg+xml;base64,${base64.encode(svg_str)}`的方式进行插入。
至于点击事件,我通过一些不太优美的方式,使用EditorContext.getBounds实现了Editor点击图片触发事件。(在我的回答里有相关实现的思路)
async insertLink() {
const { edit_index, link_text, link_url } = this.state
if (edit_index >= 0) {
await this.props.getEditorContext().deleteText({
index: edit_index,
length: 1,
await this.props.getEditorContext().setSelection({
length: 0,
let link_info = TexUtil.generateTexInfo(`\\underline{${link_text}}`)
// console.log(link_info)
this.props.getEditorContext().insertImage({
extClass: link_info.extClass,
src: link_info.src,
nowrap: true,
alt: link_text,
data: {
data: WXEditorHelper.encodeDataCustom({
type: 'link',
href: link_url,
text: link_text
},
width: link_info.width,
height: link_info.height,
console.log('link ext class:', link_info.extClass)
this.closeLinkDialog()
async onClickEditor(e) {
const { x, y } = e.detail
// console.log(x, y)
let data_res = await this.editor_context.getContents()
// console.log(data_res)
let all_length = this.getWxDeltaLength(data_res.delta)
// console.log('all_length:', all_length)
// 二分法应该可以优化,规模小暂时不优化
for (let i = 0; i < all_length; i++) {
let bounds_res = await this.editor_context.getBounds({
index: i,
let bounds = bounds_res.bounds
// console.log(bounds)
if (bounds.left <= x && x <= bounds.left + bounds.width &&
bounds.top <= y && y <= bounds.top + bounds.height
) {
// console.log('click on index:', i)
let item_type = this.getWxDeltaIndexType(data_res.delta, i)
// console.log('click on type:', item_type)
if (item_type.type != 'text') {
this.onClickItem(i, item_type.type, item_type.data, item_type)
break
@maxWidth: 100;
@maxHeight: 100;
@minVerticalAlign: -100;
@maxVerticalAlign: 100;
// 下面width、height、verticalAlign均为公式图片所需样式
// 批量生成宽度样式
.generateWidth(@n) when (@n > 0) {
.width-@{n}em {
width: (@n / 10em);
.generateWidth(@n - 1);
.generateWidth(@maxWidth);
// 批量生成高度样式
.generateHeight(@n) when (@n > 0) {
.height-@{n}em {
height: (@n / 10em);
.generateHeight(@n - 1);
.generateHeight(@maxHeight);
// 批量生成对齐样式
.generateVerticalAlign(@n) when (@n > @minVerticalAlign) {
.verticalAlign-@{n}em {
vertical-align: (@n / 10em);
.generateVerticalAlign(@n - 1);
.generateVerticalAlign(@maxVerticalAlign);
本回答由AI生成,可能已过期、失效或不适用于当前情形,请谨慎参考
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
insertLink() { const that = this wx.showModal({ title: '插入链接', editable: true, placeholderText: '输入链接地址(包含http://)', success(urlRes) { if (urlRes.confirm) { wx.showModal({ title: '链接文字', editable: true, placeholderText: '输入显示文字', success(textRes) { if (textRes.confirm) { // 插入定位标识 that.editorCtx.insertText({ text: '#-*=*-*=*-*=*@-link超链接标识link-@*=*-*=*-*=*-#', success() { // 获取全文内容 that.editorCtx.getContents({ success(res) { const ops = res.delta.ops const findex = ops.findIndex(item => { return item.insert && item.insert.includes('link超链接标识link') }) if (findex > -1) { const original = ops[findex].insert const attrs = ops[findex].attributes || {} const [prefix, suffix] = original.split('#-*=*-*=*-*=*@-link超链接标识link-@*=*-*=*-*=*-#') // 构建新操作 const newOps = [] if (prefix) newOps.push({ insert: prefix, attributes: attrs }) newOps.push({ insert: textRes.content, attributes: { link: urlRes.content, color: '#007AFF', textDecoration: 'underline' } }) if (suffix) newOps.push({ insert: suffix, attributes: attrs }) // 更新编辑器内容 ops.splice(findex, 1, ...newOps) that.editorCtx.setContents({ delta: { ops }, success() { that.editorCtx.blur() } }) } } }) } }) } } }) } } }) },这个是添加链接的方法,你试一试 看看能不能满足需要
function handleHtmlWithVideo(html) {// 正则表达式用于匹配img标签中带有alt属性且alt属性值为视频链接的模式const regex = /<img\s+src="[^"]*"\s+alt="([^"]*)"[^>]*>/g// 使用replace方法和一个函数回调来替换匹配到的内容return html.replace(regex, (match, videoUrl) => {// 替换为video标签,并添加controls属性以便用户可以控制播放return `<video width="80%" controls><source src="${videoUrl}" type="video/mp4"></video>`})}我通过insertImage接口实现了插入图片、超链接、表格、公式的功能。
是的,所有上述我都通过svg图片渲染出来,通过定义data-custom数据结构保存相关属性,在里面定义type字段来区分不同元素。理论上视频也可以通过这种方式实现,渲染一个封面图或者svg图片,点击可以打开一个弹窗,播放或者修改视频。不过最终生成的html需要再额外处理一下,把里面的img标签通过data-custom字段来转换回真实的标签类型。
svg图片可以通过 src: `data:image/svg+xml;base64,${base64.encode(svg_str)}`的方式进行插入。
至于点击事件,我通过一些不太优美的方式,使用EditorContext.getBounds实现了Editor点击图片触发事件。(在我的回答里有相关实现的思路)
async insertLink() {const { edit_index, link_text, link_url } = this.stateif (edit_index >= 0) {await this.props.getEditorContext().deleteText({index: edit_index,length: 1,})await this.props.getEditorContext().setSelection({index: edit_index,length: 0,})}let link_info = TexUtil.generateTexInfo(`\\underline{${link_text}}`)// console.log(link_info)this.props.getEditorContext().insertImage({extClass: link_info.extClass,src: link_info.src,nowrap: true,alt: link_text,data: {data: WXEditorHelper.encodeDataCustom({type: 'link',href: link_url,text: link_text})},width: link_info.width,height: link_info.height,})console.log('link ext class:', link_info.extClass)this.closeLinkDialog()}async onClickEditor(e) {const { x, y } = e.detail// console.log(x, y)let data_res = await this.editor_context.getContents()// console.log(data_res)let all_length = this.getWxDeltaLength(data_res.delta)// console.log('all_length:', all_length)// 二分法应该可以优化,规模小暂时不优化for (let i = 0; i < all_length; i++) {let bounds_res = await this.editor_context.getBounds({index: i,length: 1,})let bounds = bounds_res.bounds// console.log(bounds)if (bounds.left <= x && x <= bounds.left + bounds.width &&bounds.top <= y && y <= bounds.top + bounds.height) {// console.log('click on index:', i)let item_type = this.getWxDeltaIndexType(data_res.delta, i)// console.log('click on type:', item_type)if (item_type.type != 'text') {this.onClickItem(i, item_type.type, item_type.data, item_type)}break}}}@maxWidth: 100;@maxHeight: 100;@minVerticalAlign: -100;@maxVerticalAlign: 100;// 下面width、height、verticalAlign均为公式图片所需样式// 批量生成宽度样式.generateWidth(@n) when (@n > 0) {.width-@{n}em {width: (@n / 10em);}.generateWidth(@n - 1);}.generateWidth(@maxWidth);// 批量生成高度样式.generateHeight(@n) when (@n > 0) {.height-@{n}em {height: (@n / 10em);}.generateHeight(@n - 1);}.generateHeight(@maxHeight);// 批量生成对齐样式.generateVerticalAlign(@n) when (@n > @minVerticalAlign) {.verticalAlign-@{n}em {vertical-align: (@n / 10em);}.generateVerticalAlign(@n - 1);}.generateVerticalAlign(@maxVerticalAlign);length: 1,