Repository navigation
Add inline choice chip and Insert action to the passage editor #6277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rtibblesbot
wants to merge
1
commit into
learningequality:unstable
Choose a base branch
from
rtibblesbot:issue-6181-538178
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,575
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
229 changes: 229 additions & 0 deletions
229
...entcuration/frontend/shared/views/QTIEditor/interactions/inlineChoice/InlineChoiceNode.js
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| // The resolver does not read package `exports` subpaths. | ||
| /* eslint-disable import/no-unresolved */ | ||
| import { isHistoryTransaction } from '@tiptap/pm/history'; | ||
| import { Fragment, Slice } from '@tiptap/pm/model'; | ||
| import { NodeSelection, Plugin } from '@tiptap/pm/state'; | ||
| /* eslint-enable import/no-unresolved */ | ||
| import { | ||
| Node, | ||
| combineTransactionSteps, | ||
| findChildren, | ||
| findChildrenInRange, | ||
| getChangedRanges, | ||
| } from '@tiptap/core'; | ||
| import { VueNodeViewRenderer } from '@tiptap/vue-2'; | ||
| import { generateRandomSlug } from '../../utils/generateRandomSlug'; | ||
| import InlineChoiceChip from './components/InlineChoiceChip'; | ||
| import { CORRECT_ATTR, DROPDOWN, OPTION, readDropdown } from './parse'; | ||
|
|
||
| const NODE_NAME = 'inlineChoice'; | ||
|
|
||
| const isChip = node => node.type.name === NODE_NAME; | ||
|
|
||
| export const findChip = (doc, responseIdentifier) => | ||
| findChildren( | ||
| doc, | ||
| node => isChip(node) && node.attrs.responseIdentifier === responseIdentifier, | ||
| )[0]; | ||
|
|
||
| function* chipsIn(fragment) { | ||
| for (let i = 0; i < fragment.childCount; i++) { | ||
| const child = fragment.child(i); | ||
| if (isChip(child)) yield child; | ||
| else yield* chipsIn(child.content); | ||
| } | ||
| } | ||
|
|
||
| function idsOf(attrs) { | ||
| return [attrs.responseIdentifier, ...attrs.options.map(option => option.id)]; | ||
| } | ||
|
|
||
| const claimIds = (taken, attrs) => idsOf(attrs).forEach(id => taken.add(id)); | ||
|
|
||
| // A missing id needs a fresh one too: the save leaves it missing. | ||
| function needsFreshIds(attrs, taken) { | ||
| const ids = idsOf(attrs); | ||
| return new Set(ids).size !== ids.length || ids.some(id => !id || taken.has(id)); | ||
| } | ||
|
|
||
| // Renames only the missing and taken ids, so a cut-paste keeps the chip's identity. | ||
| function withFreshIds({ responseIdentifier, options, correctId }, taken) { | ||
| const seen = new Set(taken); | ||
| const fresh = (id, prefix) => { | ||
| if (!id || seen.has(id)) return generateRandomSlug(prefix); | ||
| seen.add(id); | ||
| return id; | ||
| }; | ||
| const renamed = { responseIdentifier: fresh(responseIdentifier, 'response') }; | ||
| renamed.options = options.map(option => ({ ...option, id: fresh(option.id, 'choice') })); | ||
| const correctIndex = options.findIndex(option => option.id === correctId); | ||
| renamed.correctId = correctIndex === -1 ? null : renamed.options[correctIndex].id; | ||
| return renamed; | ||
| } | ||
|
|
||
| const isSpace = (doc, pos) => /\s/.test(doc.textBetween(pos, pos + 1)); | ||
|
|
||
| function trimmedRange({ doc, selection }) { | ||
| let { from, to } = selection; | ||
| while (from < to && isSpace(doc, from)) from++; | ||
| while (to > from && isSpace(doc, to - 1)) to--; | ||
| return { from, to }; | ||
| } | ||
|
|
||
| function reidentify(fragment, taken) { | ||
| const children = []; | ||
| fragment.forEach(child => { | ||
| if (isChip(child)) { | ||
| const node = needsFreshIds(child.attrs, taken) | ||
| ? child.type.create(withFreshIds(child.attrs, taken)) | ||
| : child; | ||
| claimIds(taken, node.attrs); | ||
| children.push(node); | ||
| } else { | ||
| children.push(child.copy(reidentify(child.content, taken))); | ||
| } | ||
| }); | ||
| return Fragment.fromArray(children); | ||
| } | ||
|
|
||
| export const InlineChoiceNode = Node.create({ | ||
| name: NODE_NAME, | ||
| group: 'inline', | ||
| inline: true, | ||
| atom: true, | ||
|
|
||
| addAttributes() { | ||
| return { | ||
| responseIdentifier: { default: '', rendered: false }, | ||
| options: { default: [], rendered: false }, | ||
| correctId: { default: null, rendered: false }, | ||
| }; | ||
| }, | ||
|
|
||
| parseHTML() { | ||
| return [{ tag: DROPDOWN, getAttrs: readDropdown }]; | ||
| }, | ||
|
|
||
| renderHTML({ node }) { | ||
| const { responseIdentifier, options, correctId } = node.attrs; | ||
| const attrs = { 'response-identifier': responseIdentifier }; | ||
| if (correctId !== null) attrs[CORRECT_ATTR] = correctId; | ||
| return [ | ||
| DROPDOWN, | ||
| attrs, | ||
| ...options.map(option => [OPTION, { identifier: option.id }, option.text]), | ||
| ]; | ||
| }, | ||
|
|
||
| addCommands() { | ||
| return { | ||
| // The selected text becomes the chip's correct option. With no text, or with an inline | ||
| // atom (a chip, a formula) that plain text cannot hold, the chip goes after the selection, | ||
| // so nothing is deleted. | ||
| insertInlineChoice: | ||
| responseIdentifier => | ||
| ({ tr, commands }) => { | ||
| const { from, to } = trimmedRange(tr); | ||
| const { selection } = tr; | ||
| let hasAtom = false; | ||
| tr.doc.nodesBetween(selection.from, selection.to, node => { | ||
| if (node.isInline && !node.isText) hasAtom = true; | ||
| return !hasAtom; | ||
| }); | ||
| const text = hasAtom ? '' : tr.doc.textBetween(from, to); | ||
| const option = { id: generateRandomSlug('choice'), text }; | ||
| const attrs = { | ||
| responseIdentifier, | ||
| options: [option], | ||
| correctId: text ? option.id : null, | ||
| }; | ||
| // Places the cursor after the chip, even when it has to be wrapped in a new paragraph. | ||
| return commands.insertContentAt(text ? { from, to } : selection.to, { | ||
| type: NODE_NAME, | ||
| attrs, | ||
| }); | ||
| }, | ||
| // A chip needs at least one option, each with an id, to be schema-valid. | ||
| updateInlineChoice: | ||
| (responseIdentifier, { options, correctId }) => | ||
| ({ tr, state, dispatch }) => { | ||
| const found = findChip(state.doc, responseIdentifier); | ||
| if (!found || (options && !options.length)) return false; | ||
| const attrs = { ...found.node.attrs }; | ||
| if (options) { | ||
| attrs.options = options.map(option => | ||
| option.id ? option : { ...option, id: generateRandomSlug('choice') }, | ||
| ); | ||
| } | ||
| if (correctId !== undefined) attrs.correctId = correctId; | ||
| if (!attrs.options.some(option => option.id === attrs.correctId)) attrs.correctId = null; | ||
| if (dispatch) { | ||
| const wasSelected = tr.selection.node && tr.selection.from === found.pos; | ||
| // Replaces the chip, so history groups quick successive edits as it does typing. | ||
| tr.setNodeMarkup(found.pos, undefined, attrs); | ||
| if (wasSelected) tr.setSelection(NodeSelection.create(tr.doc, found.pos)); | ||
| } | ||
| return true; | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| addNodeView() { | ||
| return VueNodeViewRenderer(InlineChoiceChip); | ||
| }, | ||
|
|
||
| addProseMirrorPlugins() { | ||
| return [ | ||
| new Plugin({ | ||
| props: { | ||
| // A copy must not share ids with the original; a cut-paste has already removed them. | ||
| transformPasted(slice, view) { | ||
| // Whether a drag moves or copies is only known at drop; see `appendTransaction`. | ||
| if (view.dragging || chipsIn(slice.content).next().done) return slice; | ||
| const taken = new Set(); | ||
| for (const { node } of findChildren(view.state.doc, isChip)) | ||
| claimIds(taken, node.attrs); | ||
| return new Slice(reidentify(slice.content, taken), slice.openStart, slice.openEnd); | ||
| }, | ||
| }, | ||
| // Catches inserts `transformPasted` does not see, such as a drop. An inserted chip whose | ||
| // ids are still in use elsewhere was copied, not moved. | ||
| appendTransaction(transactions, oldState, newState) { | ||
| // Undo and redo only restore states this plugin already accepted. | ||
| if (transactions.every(isHistoryTransaction)) return null; | ||
| if (!transactions.some(tr => tr.docChanged)) return null; | ||
| const changed = getChangedRanges(combineTransactionSteps(oldState.doc, transactions)); | ||
| const inserted = new Map(); | ||
| for (const { newRange } of changed) { | ||
| for (const { node, pos } of findChildrenInRange(newState.doc, newRange, isChip)) { | ||
| inserted.set(pos, node); | ||
| } | ||
| } | ||
| if (!inserted.size) return null; | ||
| const taken = new Set(); | ||
| for (const { node, pos } of findChildren(newState.doc, isChip)) { | ||
| if (!inserted.has(pos)) claimIds(taken, node.attrs); | ||
| } | ||
| // An edited or moved chip keeps its ids unless another chip has its response identifier, | ||
| // which only a copy shares; repeated choice ids are left to the save. | ||
| const existing = new Set( | ||
| findChildren(oldState.doc, isChip).map(({ node }) => node.attrs.responseIdentifier), | ||
| ); | ||
| let tr = null; | ||
| for (const [pos, node] of [...inserted].sort(([a], [b]) => a - b)) { | ||
| let { attrs } = node; | ||
| const stale = existing.has(attrs.responseIdentifier) | ||
| ? taken.has(attrs.responseIdentifier) | ||
| : needsFreshIds(attrs, taken); | ||
| if (stale) { | ||
| attrs = withFreshIds(attrs, taken); | ||
| tr = (tr || newState.tr).setNodeMarkup(pos, undefined, attrs); | ||
| } | ||
| claimIds(taken, attrs); | ||
| } | ||
| return tr; | ||
| }, | ||
| }), | ||
| ]; | ||
| }, | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: This is the subtlest code in the PR: an inserted chip whose response id existed before is a copy only if another chip still holds that id. Only QA steps 7–8 cover the drop path (drag-move vs Ctrl+drag). It doesn't need a simulated drag to test. Dispatching a transaction that inserts a copy of an existing chip (expect fresh ids, correct choice remapped) and one that deletes and reinserts it elsewhere in the same transaction (expect ids kept) exercises exactly this branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 85e7194 ("inserted by a transaction"): one dispatch inserts a copy of a chip still in the passage (fresh ids, correct choice remapped); one deletes and reinserts it in the same transaction (ids kept). The moved chip holds an id-less option, so only the "response id existed before and no other chip holds it" branch keeps its ids — checked by disabling that branch, which fails the test.