Skip to content
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

GN-4732: ensure variables are selected/focused after insertion #392

Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/wet-baboons-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lblod/ember-rdfa-editor-lblod-plugins": minor
---

Ensure that variables are node-selected/focused after insertion
1 change: 1 addition & 0 deletions addon/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as replaceSelectionWithAndSelectNode } from './replace-selection-and-select-node';
24 changes: 24 additions & 0 deletions addon/commands/replace-selection-and-select-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Command, NodeSelection, PNode } from '@lblod/ember-rdfa-editor';

const replaceSelectionWithAndSelectNode = (node: PNode): Command => {
return (state, dispatch) => {
if (!node.type.spec.selectable) {
return false;
}

if (dispatch) {
const tr = state.tr;
tr.replaceSelectionWith(node);
if (tr.selection.$anchor.nodeBefore) {
const resolvedPos = tr.doc.resolve(
tr.selection.anchor - tr.selection.$anchor.nodeBefore?.nodeSize,
);
tr.setSelection(new NodeSelection(resolvedPos));
}
dispatch(tr);
}
return true;
};
};

export default replaceSelectionWithAndSelectNode;
13 changes: 5 additions & 8 deletions addon/components/variable-plugin/codelist/insert.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { SayController } from '@lblod/ember-rdfa-editor';
import { NodeSelection, SayController } from '@lblod/ember-rdfa-editor';
import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
import {
CodeList,
Expand All @@ -16,6 +16,7 @@ import {
EXT,
RDF,
} from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { replaceSelectionWithAndSelectNode } from '@lblod/ember-rdfa-editor-lblod-plugins/commands';

export type CodelistInsertOptions = {
publisher?: string;
Expand Down Expand Up @@ -141,13 +142,9 @@ export default class CodelistInsertComponent extends Component<Args> {
);

this.label = undefined;

this.controller.withTransaction(
(tr) => {
return tr.replaceSelectionWith(node);
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}

@action
Expand Down
13 changes: 5 additions & 8 deletions addon/components/variable-plugin/date/insert-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { SayController, Transaction } from '@lblod/ember-rdfa-editor';
import { SayController } from '@lblod/ember-rdfa-editor';
import { v4 as uuidv4 } from 'uuid';
import IntlService from 'ember-intl/services/intl';
import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
Expand All @@ -12,6 +12,7 @@ import {
RDF,
XSD,
} from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { replaceSelectionWithAndSelectNode } from '@lblod/ember-rdfa-editor-lblod-plugins/commands';

type Args = {
controller: SayController;
Expand Down Expand Up @@ -75,12 +76,8 @@ export default class DateInsertVariableComponent extends Component<Args> {
});

this.label = undefined;

this.controller.withTransaction(
(tr: Transaction) => {
return tr.replaceSelectionWith(node);
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
14 changes: 5 additions & 9 deletions addon/components/variable-plugin/date/insert.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { SayController, Transaction } from '@lblod/ember-rdfa-editor';
import { SayController } from '@lblod/ember-rdfa-editor';
import IntlService from 'ember-intl/services/intl';
import { v4 as uuidv4 } from 'uuid';
import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
Expand All @@ -10,6 +10,7 @@ import {
EXT,
RDF,
} from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { replaceSelectionWithAndSelectNode } from '@lblod/ember-rdfa-editor-lblod-plugins/commands';

type Args = {
controller: SayController;
Expand Down Expand Up @@ -65,13 +66,8 @@ export default class DateInsertComponent extends Component<Args> {
],
backlinks: [],
});
console.log(node);

this.controller.withTransaction(
(tr: Transaction) => {
return tr.replaceSelectionWith(node);
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
11 changes: 4 additions & 7 deletions addon/components/variable-plugin/location/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SayController } from '@lblod/ember-rdfa-editor';
import { service } from '@ember/service';
import IntlService from 'ember-intl/services/intl';
import { v4 as uuidv4 } from 'uuid';
import { replaceSelectionWithAndSelectNode } from '@lblod/ember-rdfa-editor-lblod-plugins/commands';

export type LocationInsertOptions = {
endpoint: string;
Expand Down Expand Up @@ -63,12 +64,8 @@ export default class LocationInsertComponent extends Component<Args> {
);

this.label = undefined;

this.controller.withTransaction(
(tr) => {
return tr.replaceSelectionWith(node);
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
10 changes: 4 additions & 6 deletions addon/components/variable-plugin/number/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EXT,
RDF,
} from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { replaceSelectionWithAndSelectNode } from '@lblod/ember-rdfa-editor-lblod-plugins/commands';

type Args = {
controller: SayController;
Expand Down Expand Up @@ -134,11 +135,8 @@ export default class NumberInsertComponent extends Component<Args> {
this.minimumValue = '';
this.maximumValue = '';

this.controller.withTransaction(
(tr) => {
return tr.replaceSelectionWith(node);
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
12 changes: 5 additions & 7 deletions addon/components/variable-plugin/text/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { service } from '@ember/service';
import type { SayController } from '@lblod/ember-rdfa-editor';
import { type SayController } from '@lblod/ember-rdfa-editor';
import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
import { v4 as uuidv4 } from 'uuid';
import IntlService from 'ember-intl/services/intl';
Expand All @@ -11,6 +11,7 @@ import {
EXT,
RDF,
} from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { replaceSelectionWithAndSelectNode } from '@lblod/ember-rdfa-editor-lblod-plugins/commands';

type Args = {
controller: SayController;
Expand Down Expand Up @@ -81,11 +82,8 @@ export default class TextVariableInsertComponent extends Component<Args> {

this.label = undefined;

this.controller.withTransaction(
(tr) => {
return tr.replaceSelectionWith(node);
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
1 change: 1 addition & 0 deletions addon/plugins/variable-plugin/variables/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ const emberNodeConfig: EmberNodeConfig = {
uriAttributes: ['variableInstance'],
draggable: false,
needsFFKludge: true,
selectable: true,
attrs: {
...rdfaAttrSpec,
value: {
Expand Down
1 change: 1 addition & 0 deletions addon/plugins/variable-plugin/variables/codelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const emberNodeConfig: EmberNodeConfig = {
uriAttributes: ['variableInstance'],
draggable: false,
needsFFKludge: true,
selectable: true,
attrs: {
...rdfaAttrSpec,
selectionStyle: {
Expand Down
1 change: 1 addition & 0 deletions addon/plugins/variable-plugin/variables/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const emberNodeConfig: EmberNodeConfig = {
content: 'inline*',
atom: true,
recreateUri: true,
selectable: true,
uriAttributes: ['variableInstance'],
draggable: false,
needsFFKludge: true,
Expand Down
1 change: 1 addition & 0 deletions addon/plugins/variable-plugin/variables/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ const emberNodeConfig: EmberNodeConfig = {
editable: true,
draggable: false,
needsFFKludge: true,
selectable: true,
attrs: {
...rdfaAttrSpec,
writtenNumber: { default: false },
Expand Down
1 change: 1 addition & 0 deletions addon/plugins/variable-plugin/variables/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const emberNodeConfig: EmberNodeConfig = {
draggable: false,
needsFFKludge: true,
editable: true,
selectable: true,
attrs: {
...rdfaAttrSpec,
},
Expand Down