Skip to content

Commit

Permalink
introduce seperate replace-and-select command
Browse files Browse the repository at this point in the history
  • Loading branch information
elpoelma committed Feb 29, 2024
1 parent d23e0bb commit 8f942f2
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 96 deletions.
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;
18 changes: 4 additions & 14 deletions addon/components/variable-plugin/codelist/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,20 +142,9 @@ export default class CodelistInsertComponent extends Component<Args> {
);

this.label = undefined;

this.controller.withTransaction(
(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));
}
return tr;
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}

@action
Expand Down
24 changes: 5 additions & 19 deletions addon/components/variable-plugin/date/insert-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { service } from '@ember/service';
import {
NodeSelection,
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 @@ -16,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 @@ -79,19 +76,8 @@ export default class DateInsertVariableComponent extends Component<Args> {
});

this.label = undefined;

this.controller.withTransaction(
(tr: Transaction) => {
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));
}
return tr;
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
25 changes: 5 additions & 20 deletions addon/components/variable-plugin/date/insert.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';
import {
NodeSelection,
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 @@ -14,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 @@ -69,20 +66,8 @@ export default class DateInsertComponent extends Component<Args> {
],
backlinks: [],
});
console.log(node);

this.controller.withTransaction(
(tr: Transaction) => {
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));
}
return tr;
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
20 changes: 5 additions & 15 deletions addon/components/variable-plugin/location/insert.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { NodeSelection, SayController } from '@lblod/ember-rdfa-editor';
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,19 +64,8 @@ export default class LocationInsertComponent extends Component<Args> {
);

this.label = undefined;

this.controller.withTransaction(
(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));
}
return tr;
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
19 changes: 5 additions & 14 deletions addon/components/variable-plugin/number/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 { NodeSelection, SayController } from '@lblod/ember-rdfa-editor';
import { SayController } from '@lblod/ember-rdfa-editor';
import { v4 as uuidv4 } from 'uuid';
import { isNumber } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/strings';
import { service } from '@ember/service';
Expand All @@ -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,18 +135,8 @@ export default class NumberInsertComponent extends Component<Args> {
this.minimumValue = '';
this.maximumValue = '';

this.controller.withTransaction(
(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));
}
return tr;
},
{ view: this.controller.mainEditorView },
);
this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
view: this.controller.mainEditorView,
});
}
}
19 changes: 5 additions & 14 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 { NodeSelection, 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,18 +82,8 @@ export default class TextVariableInsertComponent extends Component<Args> {

this.label = undefined;

this.controller.withTransaction(
(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));
}
return tr;
},
{ 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

0 comments on commit 8f942f2

Please sign in to comment.