Skip to content
ФреймворкJavaScript

Saver API: сериализация редактора в JSON

Сохранение и экспорт содержимого редактора.

Как получить экземпляр редактора

Методы ниже вызываются на редакторе, созданном через new Blok(). Они доступны после того, как разрешится editor.isReady.

TypeScript
// You already hold the instance returned by the constructor.
const editor = new Blok({ holder: 'editor' });
await editor.isReady;

// Call any API method on it.
editor.caret.setToLastBlock('end');

Методы

saver.save()

Promise<OutputData>

Alias for the main save() method. **Rejects while read-only mode is enabled** — it warns and throws `Error("Blok's content can not be saved in read-only mode")` before reaching the Saver. Guard the call with `editor.readOnly.isEnabled`, or call `await editor.readOnly.set(false)` first.

Когда использовать

Идентичен editor.save(); для кода, работающего через модуль saver. Оба дожидаются полного дерева блоков.

Ошибки

  • Read-only mode is enabled (editor.readOnly.isEnabled === true).

    Blok's content can not be saved in read-only mode

    Disable read-only with readOnly.set(false) before saving, or gate the call on readOnly.isEnabled.

  • Collecting the data failed (a tool's save() threw).

    Blok's content can not be saved because collecting data failed — or the originating tool error, re-thrown when one was recorded

    Inspect the rejected error: it is the tool's own error whenever the Saver recorded one.

TypeScript
if (!editor.readOnly.isEnabled) {
  const data = await editor.saver.save();
  // Returns: { version, time, blocks }
}