检测 HTML 文本字段中发生的更改

发布于 2024-08-27 13:56:30 字数 447 浏览 2 评论 0原文

对于一个主要的学校项目,我正在实施一个实时协作编辑器。简单介绍一下背景知识,这基本上意味着两个(或更多)用户可以同时在文档中键入内容,并且他们的更改会自动传播到彼此(类似于 Etherpad)。

现在我的问题如下:

我希望能够检测用户对 HTML 文本字段进行了哪些更改。他们可以:

  • 插入一个字符
  • 删除一个字符
  • 粘贴一串字符
  • 剪切一串字符

我希望能够检测到发生了哪些更改,然后通知其他客户端,类似于“在位置 2 插入字符‘c’”等。

不管怎样,我希望得到一些关于如何实施这些变化检测的建议?

我的第一次尝试是考虑发生变化之前和之后的胡萝卜位置,但这惨败了。

对于我的第二次尝试,我正在考虑对文本字段旧值和新值的全部内容进行比较。我是否遗漏了这个解决方案的任何明显内容?有没有更简单的东西?

For a major school project I am implementing a real-time collaborative editor. For a little background, basically what this means is that two(or more) users can type into a document at the same time, and their changes are automatically propagated to one another (similar to Etherpad).

Now my problem is as follows:

I want to be able to detect what changes a user carried out onto an HTML textfield. They could:

  • Insert a character
  • Delete a character
  • Paste a string of characters
  • Cut a string of characters

I want to be able to detect which of these changes happened and then notify other clients similar to "insert character 'c' at position 2" etc.

Anyway I was hoping to get some advice on how I would go about implementing the detection of these changes?

My first attempt was to consider the carot position before and after a change occurred, but this failed miserably.

For my second attempt I was thinking about doing a diff on the entire contents of the textfields old and new value. Am I missing anything obvious with this solution? Is there something simpler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-09-03 13:56:30

由于多种原因,今天要使其正常工作确实是一项艰巨的工作,但

一个个人注意:检测单词、字符的变化可能完全没有意义,也没有用,而是检测段落变化,或者在类似 excel 的工作表中,单个单元格,

我希望这有助于

随意纠正我的英语!

It is a really hard work make this working today, for several reasons, but

one personal note: detecting words, characters changes may be totally non-sense and not useful, detect instead paragraphs changes, or in case of an excel-like worksheet, the single cell

I hope this helps

feel free to correct my English!

画骨成沙 2024-09-03 13:56:30

我的伪代码/写出的响应将是(如果我完全理解你的问题)使用 jQuery 来检测 keyup 事件,然后通过 ajax 将输入保存到服务器,然后还获取响应并将其发布回输入。这不是很有效,但基本上的想法是您不断发布并检查其他已发布的内容。如果您想实时查看其他人在做什么,您可以每隔一秒左右对服务器执行一次 ping 操作,并使用响应进行更新。

所有这些当然都可以优化,但对服务器来说仍然是一种负担。您还可以看看是否可以为您的项目实施 Google Topeka Wave,或者与 Google Topeka 联系,看看他们是如何做到的:)

My pseudocode/written out response would be (if I understand your question exactly) to use jQuery to detect keyup events and then save the input to the server via ajax, then also take the response and post it back to the input. This isn't very efficient, but basically the idea is that you're constantly posting and checking what else has been posted. If you want to see what someone else is doing in real time, you can ping the server every second or so and update with the response.

All of this of course can be optimized, but it still is kind of taxing for a server. You could also see if you can implement Google Topeka Wave for your project, or get in touch with Google Topeka to see how they do it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文