惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
大猫的无限游戏
大猫的无限游戏
NISL@THU
NISL@THU
Scott Helme
Scott Helme
G
Google Developers Blog
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Exploit Database - CXSecurity.com
G
GRAHAM CLULEY
The Cloudflare Blog
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
N
Netflix TechBlog - Medium
Hacker News: Ask HN
Hacker News: Ask HN
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cisco Blogs
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Forbes - Security
Forbes - Security
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
S
Security Affairs
B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Privacy & Cybersecurity Law Blog
Martin Fowler
Martin Fowler
Know Your Adversary
Know Your Adversary
F
Full Disclosure
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
博客园 - Franky
F
Fortinet All Blogs
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
L
Lohrmann on Cybersecurity
T
Tenable Blog
V
V2EX
W
WeLiveSecurity
Google Online Security Blog
Google Online Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed

博客园 - BloggerSb

Swagger 文档设置api版本 .Net Core Routing Demo .net Core读取配置比较 简化版DbExecutor,将DataTable映射到T属性(支持Dapper风格的匿名参数)。(编程题) 大文件单词统计 (编程题) ASP.NET Core CRUD API 创建 UserController,实现 Get, Post, Put, Delete 方法,使用 EF Core 访问数据库。 (编程题) 设计模式落地:Repository + UnitOfWork + CQRS 完整实现 (编程题) 给定百万级订单表,实现高效分页 + 动态条件查询 + 导出 Excel(避免内存爆炸) (编程题) 实现一个带 CorrelationId、请求日志、异常统一处理的中间件链 (编程题) 异步限流器实现(编程题) 编程题,记录所有接口的执行耗时 .net面试题目 (问答题) 面试高频简答题 Aspose最新Slides破解 HttpContext.User.Identity.IsAuthenticated 为false 关于Cannot resolve scoped service from root provider解决方案 MongoDB用户权限管理,设置密码并连接 mongodb连接字符串 mongodb 使用 MongoDB Compass 创建账号,角色 安装mongodb bootstrap popover 设置悬浮框宽度 div contenteditable="true" 添加placehoder效果
光标自动定位到起始位置contenteditable="true" ,v-html绑定内容,div可编辑时,光标移到最前面
BloggerSb · 2024-03-03 · via 博客园 - BloggerSb

出现这个问题原因:

(1)通过打断点可以看到,当你输入的时候触发input事件,提交值给父组件中的v-model;

(2)但因为在子组件中又监听了v-model的值,所以整体形成了闭环;

(3)还需要重点说明的是光标问题,contenteditable与v-html所在的元素值的改变如果不是通过输入而是通过赋值实现,光标就会跑到最前面;

所以以输入中文为例,你刚打了一个字母,立马就触发了监听与变动,光标移到最前面,自然无法完成整个正常的输入。

解决办法:

只有当blur的时候再做赋值操作(isChange为true),focus状态下不做赋值(isChange为false);

至于初始为true的原因是在父组件中直接给绑定的变量赋值时子组件中还是需要触发赋值的(isChange为true);

<!-- Created by dreamsqin on 2019/9/5 -->
<template>
  <div
    class="div-editable"
    contenteditable="true"
    v-html="innerText"
    @input="changeText"
    @focus="isChange = false"
    @blur="blurFunc"></div>
</template>

<script>
  export default {
    name: 'DivEditable',
    props: {
      value: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        innerText: this.value,
        isChange: true
      }
    },
    watch: {
      value() {
        if (this.isChange) {
          this.innerText = this.value
        }
      }
    },
    methods: {
      changeText() {
        this.$emit('input', this.$el.innerHTML)
      },
      blurFunc() {
        this.isChange = true
        this.$emit('blurFunc')
      }
    }
  }
</script>

<style lang="scss">
  .div-editable{
    width: 100%;
    height: 100%;
    overflow-y: auto;
    word-break: break-all;
    outline: none;
    user-select: text;
    white-space: pre-wrap;
    text-align: left;
    &[contenteditable=true]{
      user-modify: read-write-plaintext-only;
      &:empty:before {
        content: attr(placeholder);
        display: block;
        color: #ccc;
      }
    }
  }
</style>

解决办法:

只有当blur的时候再做赋值操作(isChange为true),focus状态下不做赋值(isChange为false);

至于初始为true的原因是在父组件中直接给绑定的变量赋值时子组件中还是需要触发赋值的(isChange为true);
父组件调用

<template>
  <div class="test-page">
    <div class="contain">
      <div-editable
        v-model="testContent"
        @blurFunc="blurHighLight"></div-editable>
      <el-input
        class="input-style"
        v-model="testContent"></el-input>
      <el-button
        class="button-style"
        @click="changeText">改变值</el-button>
    </div>
  </div>
</template>

<script>
  import DivEditable from '@/components/DivEditable'
  export default {
    name: 'TestPage',
    data() {
      return {
        testContent: 'dreamsqin'
      }
    },
    components: {
      DivEditable
    },
    methods: {
      blurHighLight() {
        // 这里做数据过滤或样式变更操作
      },
      changeText() {
        this.testContent = '【标签1】dreamsqin'
        this.blurHighLight()
      }
    }
  }
</script>

<style lang="scss">
  .test-page{
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    .contain{
      width: 600px;
      height: 250px;
      border: 2px solid #000;
      .input-style,.button-style{
        margin-top: 10px;
      }
      .text-blue{
        color: #2080F7;
      }
    }
  }
</style>
<div  ref="editableDiv" class="contain"  v-html="innerText" contenteditable="true"  @input="inputText" @blur="inputBlur" @focus="inputFocus"></div>

value: {
    type: String,
    default: ''
}

innerText: this.value,
isBlur: true,

watch: {
    value() {
        if (this.isBlur) {
            this.innerText = this.value
        }
    }
},

// 监听输入框内容
inputText() {
    /*this.$emit('input', this.$refs.editor.innerHTML);*/
    this.value = this.$refs.editableDiv.innerHTML;
},
inputFocus() {
    this.isBlur = false;
},
inputBlur() {
    this.isBlur = true;
},

参照原文:
https://www.cnblogs.com/dreamsqin/p/11466197.html
https://juejin.cn/post/7089030626229092359