add comments

This commit is contained in:
idlewinn 2020-03-09 23:37:42 -07:00
parent 1419f17175
commit a0669f874e
2 changed files with 12 additions and 0 deletions

View File

@ -263,9 +263,14 @@ export class App extends React.Component<any, AppState> {
sceneAppState || getDefaultAppState(), sceneAppState || getDefaultAppState(),
{ scrollToContent: true }, { scrollToContent: true },
); );
// Perform reconciliation - in collaboration, if we encounter
// elements with more staler versions than ours, ignore them
// and keep ours.
if (elements == null || elements.length === 0) { if (elements == null || elements.length === 0) {
elements = restoredState.elements; elements = restoredState.elements;
} else { } else {
// create a map of ids so we don't have to iterate
// over the array more than once.
const elementMap = elements.reduce( const elementMap = elements.reduce(
( (
acc: { [key: string]: ExcalidrawElement }, acc: { [key: string]: ExcalidrawElement },
@ -276,6 +281,7 @@ export class App extends React.Component<any, AppState> {
}, },
{}, {},
); );
// Reconcile
elements = restoredState.elements.map(element => { elements = restoredState.elements.map(element => {
if ( if (
elementMap.hasOwnProperty(element.id) && elementMap.hasOwnProperty(element.id) &&

View File

@ -3,6 +3,9 @@ import {
MutableExcalidrawTextElement, MutableExcalidrawTextElement,
} from "./types"; } from "./types";
// This function tracks updates of text elements for the purposes for collaboration.
// The version is used to compare updates when more than one user is working in
// the same drawing.
export function mutateElement( export function mutateElement(
element: MutableExcalidrawElement, element: MutableExcalidrawElement,
callback: (mutatableElement: MutableExcalidrawElement) => void, callback: (mutatableElement: MutableExcalidrawElement) => void,
@ -11,6 +14,9 @@ export function mutateElement(
callback(element); callback(element);
} }
// This function tracks updates of text elements for the purposes for collaboration.
// The version is used to compare updates when more than one user is working in
// the same document.
export function mutateTextElement( export function mutateTextElement(
element: MutableExcalidrawTextElement, element: MutableExcalidrawTextElement,
callback: (mutatableElement: MutableExcalidrawTextElement) => void, callback: (mutatableElement: MutableExcalidrawTextElement) => void,