Last active
May 13, 2024 00:04
-
-
Save gvergnaud/631cf38804c8b5980c23f80229e31e99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TransactionCreator = (editor: EditorAPI) => Transaction; | |
const rebaseSteps = (stepsToRebase: Step[], committedSteps: Step[]): Step[] => { | |
if (!committedSteps.length) { | |
return stepsToRebase; | |
} | |
const committedStepsMaps = committedSteps.map((s) => s.getMap()); | |
const stepsToRebaseMaps = stepsToRebase.map((s) => s.getMap()); | |
// each step must be mapped to the following sequence | |
// [...inverted previous steps, ...commited steps, ...reapplied previous steps] | |
const rebasedSteps = stepsToRebase | |
.map((step, index) => { | |
const previousSteps = new Mapping( | |
stepsToRebaseMaps.slice(0, index), | |
); | |
const stepMapping = new Mapping(); | |
// Apply inverted steps | |
stepMapping.appendMappingInverted(previousSteps); | |
// Append commited mapping | |
stepMapping.appendMapping(new Mapping(committedStepsMaps)); | |
// Re apply all previous steps | |
stepMapping.appendMapping(previousSteps); | |
// Rebase current step | |
return step.map(stepMapping); | |
}) | |
.filter(filterNullish); | |
return committedSteps.concat(rebasedSteps); | |
}; | |
const rebaseCompose = | |
(f: TransactionCreator, g: TransactionCreator | null): TransactionCreator => | |
(editorAPI) => { | |
if (!g) { | |
return f(editorAPI); | |
} | |
const steps1 = f(editorAPI).steps; | |
const steps2 = g(editorAPI).steps; | |
const steps3 = rebaseSteps(steps1, steps2); | |
return steps3.reduce( | |
(transaction, step) => transaction.step(step), | |
editorAPI.state.tr, | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (transactions.some((tr) => tr.docChanged)) { | |
const changedNodeIds = new Set( | |
transactions.flatMap((t) => | |
t.steps.filter(isReplaceStep).flatMap((step) => | |
step.from === step.to | |
? [oldState.doc.resolve(step.from).node(1).attrs.cellId] | |
: oldState.doc | |
.slice(step.from, step.to) | |
.content.content.filter(isGraphOrRteCellNode) | |
.map((cell) => cell.attrs.cellId) | |
) | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment