public move()

in packages/roosterjs-react-common/lib/plugins/UndoWithImagePlugin.ts [31:68]


    public move(delta: number): string {
        if (!this.canMove(delta)) {
            return null;
        }

        const lastIndex = this.currentIndex;
        this.currentIndex += delta;
        const snapshot = this.snapshots[this.currentIndex];

        // There is a chance snapshots were saved with placeholders. To resolve that,
        // we optimistically ask Image Manager to replace the placeholders with images,
        // since the manager caches placeholder IDs to final image URLs (when they are resolved).
        // The manager returns the final HTML and also if all of the placeholders are resolved.
        if (snapshot.hasPlaceholder) {
            const originalValue = snapshot.value;
            const result: UpdatePlaceholdersResult = this.imageManager.updatePlaceholders(originalValue);
            snapshot.hasPlaceholder = !result.resolvedAll;
            snapshot.value = result.html;
            const sizeDelta = originalValue.length - result.html.length;

            // if we undo/redo and the content is the same after updating the placeholders, keep moving
            // (we get two snapshots when inserting an image, one for the placeholder, and another when the placeholder is resolved)
            const lastSnapshot = this.snapshots[lastIndex];
            if (lastSnapshot && lastSnapshot.value === snapshot.value && delta !== 0) {
                // since content is the same, remove the last "duplicated" snapshot
                this.totalSize -= snapshot.value.length;
                this.snapshots.splice(lastIndex);

                // then, move again by one unit at a time and until content is different after resolving placeholders
                return this.move(delta < 0 ? -1 : 1);
            } else {
                // it is possible total size is greater at this point (unlikely if default spinner is used)
                this.totalSize -= sizeDelta;
            }
        }

        return snapshot.value;
    }