Editor.js: Unique id for each block

Created on 25 Aug 2019  路  13Comments  路  Source: codex-team/editor.js

Hey, It would be great if editor assigned unique id for each block. This would help to keep references, add links to other blocks.
This probably would introduce breaking changes, discussion would be needed before implementing.

I have fork where I plan to do more changes, one of them is uids https://github.com/floatas/editor.js/commit/36c1f61ed66689bf9237ae3f37cc7e76e0658cb9

Most helpful comment

@mqtik to be honest I'm fine with both. As of now I will stick to UUID because this solves my personal situation and maybe the situation of some others. As mentioned in the PR I would love to receive some feedback from the maintainers.

I'm fine to make the id-generation customizable through a callback as config parameter but I would prefer to have some feedback from the maintainers in the first place instead of hanging around here with this issue.

```
const editor = new EditorJS({
customUniqueId: () => generateYourCustomId()
});

All 13 comments

This is also mentioned in #727

@floatas did you discovered any issues so far with your own implementation?

I'm also looking into this right now because it makes it more easy to post-process removed/updated blocks on the server-side.

Can you elaborate more on the breaking changes?

@floatas I have the same issue and I have added fasiha/mudderjs for generate unique ordered key. Also mentioned in (https://github.com/codex-team/editor.js/issues/819#issuecomment-506733820)

@christoph-kluge I haven't found any issues. When loading document without id's, they are generated. Seems to work with all cases, if you found any issues, let me know (also my modifications are available as npm). As for breaking changes, structure changed (added new property), I guess it should work fine with original documents, haven't tested that much.

@vincent-herlemont I'm thinking of adding ordered key to blocks, but for now I just take order of appearance and assign numbered key in backed to each block. Will definitely take a look at your implementation

@floatas thank you sharing the the code. It helped me a lot.

I discovered one issue: It always generates new uuids even if you pass an existing dataset through data while initilizing a new editor. I have extended your code with a small fix. You can find it here: https://github.com/christoph-kluge/editor.js/commit/be048d79116ce86e897c75428786c50d05b1b730 + https://github.com/christoph-kluge/editor.js/commit/6088f733f769cddb0b8f6d93636bbd17686096ac

Appreciate this is an old thread but it comes up when looking for this feature - I implemented this through introducing a "metadata" object on each block as I required to be able to assign a unique ID to every block to work with it in my backend system as well as pass additional metadata for each block through my backend system. Anyone looking for similar I have the sample code here, which can be easily customised to meet your own needs; https://github.com/alibail/editor.js

This is quite an important feature in my case.

My use case:

I want to be able to mention parts of documents from different documents. In case to do that, I need blocks to have unique, but constant id's during 'lifecycle' of any block (eg, the same id after the content of the block is changed).

I need this too.

Any updates on this?

I've added a working copy here on the current master: https://github.com/christoph-kluge/editor.js/commit/2ef5c917b4834dd03b4668063494d7fc06355142
and I've added a pull request for the next release: https://github.com/codex-team/editor.js/pull/1333

This works fine for me and my use case. Feel free to use.

Tested Features:

  • Existing data: If your previous block does not have an "id" property, it will create a new unique id
  • Existing data: If your previous block does have an "id" property, it will keep the existing unique id
  • Existing data: If a block gets changed the "id" property stays the same
  • Insert New Block: It will create a new unique id
  • Move Blocks (Up/Down): It will keep the existing unique id

Important Note:

  • If you "transform/transition" an existing block to another block it will receive a NEW unique id

I'll try it out.

Although, If not currently implemented, I'd have to make my own custom version with NanoID() (which is really fast), to keep consistent with other platforms.

@mqtik to be honest I'm fine with both. As of now I will stick to UUID because this solves my personal situation and maybe the situation of some others. As mentioned in the PR I would love to receive some feedback from the maintainers.

I'm fine to make the id-generation customizable through a callback as config parameter but I would prefer to have some feedback from the maintainers in the first place instead of hanging around here with this issue.

```
const editor = new EditorJS({
customUniqueId: () => generateYourCustomId()
});

ID is useful for React key, and so on.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oknoorap picture oknoorap  路  4Comments

hata6502 picture hata6502  路  3Comments

vincentdesmares picture vincentdesmares  路  3Comments

Yakumo-Yukari picture Yakumo-Yukari  路  5Comments

Someskumarasamy picture Someskumarasamy  路  4Comments