Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

fix(sagas): correct clause spacing and parsing - NA #68

Merged
merged 3 commits into from
Jul 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/sagas/contractSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export function* addToContract(action) {
const fromMarkdown = new FromMarkdown(pluginManager);
const toMarkdown = new ToMarkdown(pluginManager);

// Temporary fix based on the following idea:
// if you apply “fromMarkdown” to the grammar before parsing,
// both will have the same whitespace processing done and parsing will work better
// markdown <-commonmark-> markdown AST
const roundTrip = (markdownText) => {
const value = fromMarkdown.convert(markdownText);
const markdownRound = toMarkdown.convert(value);
return markdownRound;
};

// get the templateObj from the store if we already have it
// or load it and add it to the store if we do not
const templateObj = yield call(addTemplateObjectToStore, action);
Expand All @@ -54,6 +64,12 @@ export function* addToContract(action) {
const clauseMd = `\`\`\` <clause src=${action.uri} clauseId=${clauseId}>
${metadata.getSample()}
\`\`\``;

// Create a new paragraph in markdown for spacing between clauses
const paragraphSpaceMd = 'This is a new clause!';
const spacerValue = fromMarkdown.convert(paragraphSpaceMd);
const paragraphSpaceNode = spacerValue.toJSON().document.nodes[0];

const value = fromMarkdown.convert(clauseMd);
const clauseNode = value.toJSON().document.nodes[0];

Expand All @@ -62,11 +78,18 @@ export function* addToContract(action) {
const { nodes } = newSlateValue.document;

// add the clause node to the Slate dom at current position
nodes.splice(currentPosition, 0, clauseNode);
// Temporary fix to separate clauses, adding the new paragraph at
// end of splice
nodes.splice(currentPosition, 0, clauseNode, paragraphSpaceNode);

// update contract on store with new slate and md values
yield put(actions.documentEdited(Value.fromJSON(newSlateValue), newMd));
const grammar = templateObj.parserManager.getTemplatizedGrammar();

// Temporary roundtrip and rebuild grammar
const grammarRound = roundTrip(grammar);
templateObj.parserManager.buildGrammar(grammarRound);

const sampleText = templateObj.getMetadata().getSamples().default;
const model = templateObj.getModelManager().getModels();
const logic = templateObj.getScriptManager().getLogic();
Expand Down