Skip to content

Commit

Permalink
Allow disabling spacing around TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaskorz committed Nov 5, 2023
1 parent 82e5283 commit 415f9e5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
5 changes: 2 additions & 3 deletions apps/python-dsl/src/projections/incrementProjection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { arg, block, contextVariable } from "@puredit/parser";

Check warning on line 1 in apps/python-dsl/src/projections/incrementProjection.ts

View workflow job for this annotation

GitHub Actions / Deploy

'block' is defined but never used

Check warning on line 1 in apps/python-dsl/src/projections/incrementProjection.ts

View workflow job for this annotation

GitHub Actions / Deploy

'contextVariable' is defined but never used
import type { Projection } from "@puredit/projections/types";
import { pythonParser } from "./parser";
import { simpleProjection } from "@puredit/simple-projection";
import { noSpacing, simpleProjection } from "@puredit/simple-projection";

const var_x = arg("var_x", "identifier");
export const [pattern, draft] = pythonParser.statementPattern`
${var_x} += 1
`;

// todo: fix unnecessary indent
export const widget = simpleProjection([var_x, "++"]);
export const widget = simpleProjection([noSpacing(var_x), "++"]);

// alternative: explicit and long projection definion
// import { svelteProjection } from "@puredit/projections/svelte";
Expand Down
2 changes: 1 addition & 1 deletion apps/python-dsl/src/projections/putsProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const [pattern, draft] = pythonParser.statementPattern`
print(${anything}, end="")
`;

export const widget = simpleProjection(["puts ", anything]);
export const widget = simpleProjection(["puts", anything]);

// alternative projection def: explicit and long projection definion
// import { svelteProjection } from "@puredit/projections/svelte";
Expand Down
7 changes: 3 additions & 4 deletions apps/python-dsl/src/projections/timesProjection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { arg, block } from "@puredit/parser";
import type { Projection } from "@puredit/projections/types";
import { pythonParser } from "./parser";
import { simpleProjection } from "@puredit/simple-projection";
import { noSpacing, simpleProjection } from "@puredit/simple-projection";

const times_var = arg("times_var", "identifier");
const times_value = arg("times_value", "identifier");
Expand All @@ -11,11 +11,10 @@ for ${times_var} in range(${times_value}):
${block({})}
`;

// todo: fix unnecessary indent
export const widget = simpleProjection([
times_value,
noSpacing(times_value),
".times with ",
times_var,
noSpacing(times_var),
":",
]);

Expand Down
14 changes: 11 additions & 3 deletions packages/projections/TextInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export let className: string | null = null;
export let placeholder = "text";
export let focusGroup: FocusGroup | null = null;
export let noSpacing: boolean | undefined = undefined;
export let codeToValue = (code: string) => code;
export let valueToCode = (value: string) => value;
Expand Down Expand Up @@ -157,6 +158,7 @@

<label
data-value={value || placeholder}
class:no-spacing={noSpacing}
on:pointerenter={onPointerEnter}
on:pointerleave={onPointerLeave}
>
Expand Down Expand Up @@ -217,7 +219,13 @@
vertical-align: top;
align-items: center;
position: relative;
margin: 0px 0px;
margin: 0 5px;
&.no-spacing {
// The negative margin compensates the inner padding
// while still preserving the spacing towards the focus outline.
margin: 0 -4px;
}
&::after {
content: attr(data-value);
Expand Down Expand Up @@ -247,7 +255,7 @@
label::after,
input {
width: auto;
min-width: 0em;
min-width: 1em;
grid-area: 1 / 2;
font: inherit;
margin: 0;
Expand All @@ -257,7 +265,7 @@
outline: none;
display: inline-block;
padding: 0px 0px;
padding: 2px 4px;
border: 1px solid transparent;
border-radius: 3px;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/simple-projection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import TextInput from "@puredit/projections/TextInput.svelte";
import { isString } from "@puredit/utils";
import type { SvelteComponent } from "@puredit/projections/svelte";

interface SimpleProjectionArg extends TemplateArg {
noSpacing?: boolean;
}

export const simpleProjection = (
template: Array<string | TemplateArg | TemplateArg[]>
template: Array<string | SimpleProjectionArg | SimpleProjectionArg[]>
) =>
class extends ProjectionWidget implements FocusGroupHandler {
focusGroup!: FocusGroup;
Expand Down Expand Up @@ -46,6 +50,7 @@ export const simpleProjection = (
state,
view: null,
focusGroup: this.focusGroup,
noSpacing: args[0].noSpacing,
},
});
this.inputs.push([args, component]);
Expand Down Expand Up @@ -137,3 +142,8 @@ export const simpleProjection = (
});
}
};

export const noSpacing = (arg: SimpleProjectionArg): SimpleProjectionArg => ({
...arg,
noSpacing: true,
});

0 comments on commit 415f9e5

Please sign in to comment.