Skip to content

Commit

Permalink
linting and changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
lagartoverde committed Sep 9, 2024
1 parent ec6098f commit a114436
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-deers-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lblod/ember-rdfa-editor-lblod-plugins': minor
---

Add autofill variable
2 changes: 1 addition & 1 deletion addon/components/variable-plugin/autofilled/insert.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@checked={{this.convertToString}}
@onChange={{this.updateConvertToString}}
>
{{t 'variable-plugin.autofill.convertToString'}}
{{t 'variable-plugin.autofill.convertToString'}}
</AuCheckbox>
</AuFormRow>
<AuButton {{on 'click' this.insert}}>
Expand Down
4 changes: 2 additions & 2 deletions addon/components/variable-plugin/autofilled/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class AutoFilledVariableInsertComponent extends Component<Args> {
}

@action
updateConvertToString(value: boolean){
updateConvertToString(value: boolean) {
this.convertToString = value;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ export default class AutoFilledVariableInsertComponent extends Component<Args> {
},
],
autofillKey: this.autofillKey,
convertToString: this.convertToString
convertToString: this.convertToString,
},

this.schema.node('placeholder', {
Expand Down
34 changes: 17 additions & 17 deletions addon/components/variable-plugin/autofilled/nodeview.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
class='variable'
{{on 'click' this.onClick}}
>
<EmberNode::EmbeddedEditor
@controller={{@controller}}
@view={{@view}}
@getPos={{@getPos}}
@node={{@node}}
@selected={{@selected}}
@initEditor={{this.initEditor}}
@nodeViews={{@nodeViews}}
@plugins={{this.plugins}}
@decorations={{@decorations}}
@contentDecorations={{@contentDecorations}}
/>
{{#if this.label}}
<span class='label'>
({{this.label}})
</span>
{{/if}}
<EmberNode::EmbeddedEditor
@controller={{@controller}}
@view={{@view}}
@getPos={{@getPos}}
@node={{@node}}
@selected={{@selected}}
@initEditor={{this.initEditor}}
@nodeViews={{@nodeViews}}
@plugins={{this.plugins}}
@decorations={{@decorations}}
@contentDecorations={{@contentDecorations}}
/>
{{#if this.label}}
<span class='label'>
({{this.label}})
</span>
{{/if}}
</AuPill>
3 changes: 1 addition & 2 deletions addon/components/variable-plugin/autofilled/nodeview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from '@glimmer/component';
import { NodeSelection, ProsePlugin, SayView, Transaction } from '@lblod/ember-rdfa-editor';
import { NodeSelection, ProsePlugin, SayView } from '@lblod/ember-rdfa-editor';
import { editableNodePlugin } from '@lblod/ember-rdfa-editor/plugins/editable-node';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
Expand All @@ -9,7 +9,6 @@ import { EmberNodeArgs } from '@lblod/ember-rdfa-editor/utils/ember-node';
import { getOutgoingTriple } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/namespace';
import { EXT } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';


export default class AutoFilledVariableNodeViewComponent extends Component<EmberNodeArgs> {
PencilIcon = PencilIcon;

Expand Down
61 changes: 36 additions & 25 deletions addon/plugins/variable-plugin/plugins/autofiller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
Transaction,
} from '@lblod/ember-rdfa-editor';
import { changedDescendants } from '@lblod/ember-rdfa-editor/utils/_private/changed-descendants';
import {undoDepth} from '@lblod/ember-rdfa-editor/plugins/history'
import { undoDepth } from '@lblod/ember-rdfa-editor/plugins/history';

type AutofilledArgs = {
autofilledValues: {
[Key: string]: string
}
}
[Key: string]: string;
};
};

export function variableAutofillerPlugin(config: AutofilledArgs): ProsePlugin {
return new ProsePlugin({
Expand All @@ -28,9 +28,7 @@ export function variableAutofillerPlugin(config: AutofilledArgs): ProsePlugin {
newState.doc,
0,
(node: PNode, pos: number) => {
if (
node.type.name === 'autofilled_variable'
) {
if (node.type.name === 'autofilled_variable') {
autofilledVariables.push({ node, pos });
return false;
}
Expand All @@ -39,39 +37,52 @@ export function variableAutofillerPlugin(config: AutofilledArgs): ProsePlugin {
);
} else {
newState.doc.descendants((node: PNode, pos: number) => {
if (
node.type.name === 'autofilled_variable'
) {
if (node.type.name === 'autofilled_variable') {
autofilledVariables.push({ node, pos });
return false;
}
return true;
})
});
}
if (autofilledVariables.length) {
const tr = newState.tr;
autofilledVariables.reverse()
autofilledVariables.reverse();
for (const { node, pos } of autofilledVariables) {
autofillVariable(node, pos, config.autofilledValues, tr, newState.schema)
autofillVariable(
node,
pos,
config.autofilledValues,
tr,
newState.schema,
);
}
return tr;
}
return newState.tr;;
return newState.tr;
},
});
}

function autofillVariable(node: PNode, pos: number, values: {[Key: string]: string}, tr: Transaction, schema: Schema) {
function autofillVariable(
node: PNode,
pos: number,
values: { [Key: string]: string },
tr: Transaction,
schema: Schema,
) {
const autofillKey = node.attrs.autofillKey;
const value = values[autofillKey]
if(value) {
const nodeSize = node.nodeSize;
const valueNode = schema.text(value);
if(node.attrs.convertToString === true || node.attrs.convertToString === "true" ) {
tr.replaceRangeWith(pos, pos+nodeSize , valueNode)
return;
} else {
tr.replaceRangeWith(pos + 1, pos+nodeSize - 1, valueNode)
}
const value = values[autofillKey] as string;
if (value) {
const nodeSize = node.nodeSize;
const valueNode = schema.text(value);
if (
node.attrs.convertToString === true ||
node.attrs.convertToString === 'true'
) {
tr.replaceRangeWith(pos, pos + nodeSize, valueNode);
return;
} else {
tr.replaceRangeWith(pos + 1, pos + nodeSize - 1, valueNode);
}
}
}
24 changes: 9 additions & 15 deletions addon/plugins/variable-plugin/variables/autofilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const parseDOM = [
return false;
}
const autofillKey = node.dataset.autofillKey;
const convertToString = node.dataset.convertToString
const convertToString = node.dataset.convertToString;
return {
...attrs,
autofillKey,
convertToString
}
convertToString,
};
}

return false;
Expand All @@ -78,7 +78,7 @@ const parseDOM = [
const variableInstance = parseVariableInstance(node);
const label = parseLabel(node);
const autofillKey = node.dataset.autofillKey;
const convertToString = node.dataset.convertToString
const convertToString = node.dataset.convertToString;
return {
__rdfaId: uuidv4(),
subject: mappingSubject,
Expand Down Expand Up @@ -109,7 +109,7 @@ const parseDOM = [
},
],
autofillKey,
convertToString
convertToString,
};
}

Expand All @@ -126,18 +126,12 @@ const toDOM = (node: PNode): DOMOutputSpec => {
attrs: {
...node.attrs,
'data-autofill-key': node.attrs.autofillKey,
'data-convert-to-string': node.attrs.convertToString
'data-convert-to-string': node.attrs.convertToString,
},
content: 0,
});
};

type AutofilledArgs = {
autofilledValues: {
[Key: string]: string
}
}

const emberNodeConfig: EmberNodeConfig = {
name: 'autofilled-variable',
component: AutofilledNodeViewComponent as unknown as ComponentLike,
Expand All @@ -154,11 +148,11 @@ const emberNodeConfig: EmberNodeConfig = {
attrs: {
...rdfaAttrSpec({ rdfaAware }),
autofillKey: {
default: ''
default: '',
},
convertToString: {
default: false
}
default: false,
},
},
toDOM,
parseDOM,
Expand Down
2 changes: 1 addition & 1 deletion addon/plugins/variable-plugin/variables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from './location';
export * from './address';
export * from './date';
export * from './person';
export * from './autofilled'
export * from './autofilled';
8 changes: 4 additions & 4 deletions tests/dummy/app/controllers/besluit-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import {
personVariableView,
person_variable,
autofilled_variable,
autofilledVariableView
autofilledVariableView,
} from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/variables';
import {
osloLocation,
Expand Down Expand Up @@ -291,8 +291,8 @@ export default class BesluitSampleController extends Controller {
autofilledValues: {
administrativeUnit: 'Geemente Aalst',
dateRightNow: new Date().toLocaleString(),
}
}
},
},
};
}

Expand Down Expand Up @@ -322,7 +322,7 @@ export default class BesluitSampleController extends Controller {
inline_rdfa: inlineRdfaWithConfigView({ rdfaAware: true })(controller),
structure: structureView(controller),
mandatee_table: mandateeTableView(controller),
autofilled_variable: autofilledVariableView(controller)
autofilled_variable: autofilledVariableView(controller),
} satisfies Record<string, SayNodeViewConstructor>;
};
@tracked plugins: Plugin[] = [
Expand Down
9 changes: 4 additions & 5 deletions tests/dummy/app/controllers/regulatory-statement-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import {
personVariableView,
person_variable,
autofilled_variable,
autofilledVariableView
autofilledVariableView,
} from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/variables';
import { VariableConfig } from '@lblod/ember-rdfa-editor-lblod-plugins/components/variable-plugin/insert-variable-card';
import {
Expand Down Expand Up @@ -324,9 +324,8 @@ export default class RegulatoryStatementSampleController extends Controller {
endpoint: '/codex/sparql',
} as CitationPluginConfig,
autofilledVariable: {
autofilledValues: {
}
}
autofilledValues: {},
},
};
}

Expand Down Expand Up @@ -357,7 +356,7 @@ export default class RegulatoryStatementSampleController extends Controller {
inline_rdfa: inlineRdfaWithConfigView({ rdfaAware: true })(controller),
snippet_placeholder: snippetPlaceholderView(controller),
snippet: snippetView(this.config.snippet)(controller),
autofilled_variable: autofilledVariableView(controller)
autofilled_variable: autofilledVariableView(controller),
};
};
@tracked plugins: Plugin[] = [
Expand Down

0 comments on commit a114436

Please sign in to comment.