Skip to content

Commit

Permalink
Merge pull request #5 from elm-land/feature/page-codegen
Browse files Browse the repository at this point in the history
✨ Feature – Users can create stateful pages with `ElmLand.Page`
  • Loading branch information
ryan-haskell committed Jun 8, 2022
2 parents f192450 + 8da21e3 commit 12744d1
Show file tree
Hide file tree
Showing 33 changed files with 1,945 additions and 269 deletions.
2 changes: 1 addition & 1 deletion cli/package.json
@@ -1,6 +1,6 @@
{
"name": "elm-land",
"version": "0.13.1",
"version": "0.14.0",
"description": "Reliable web apps for everyone",
"main": "index.js",
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/codegen.js
Expand Up @@ -14,14 +14,14 @@ let generateElmLandFiles = async ({ pages, layouts }) => {
return newFiles
}

let addNewPage = async ({ url, filepath }) => {
let addNewPage = async ({ kind, url, filepath }) => {
let { Elm } = require('../dist/worker.js')

let newFiles = await new Promise((resolve, reject) => {
let app = Elm.Worker.init({
flags: {
tag: 'add-page',
data: { filepath, url }
data: { kind, filepath, url }
}
})
app.ports.onComplete.subscribe(resolve)
Expand Down
48 changes: 30 additions & 18 deletions cli/src/codegen/src/Commands/AddLayout.elm
Expand Up @@ -45,12 +45,17 @@ decoder =
import Html exposing (Html)
import Html.Attributes as Attr
import View exposing (View)
layout : { page : Html msg } -> Html msg
layout : { page : View msg } -> View msg
layout { page } =
Html.div
[ Attr.class "layout" ]
[ page ]
{ title = page.title
, body =
[ Html.div
[ Attr.class "layout" ]
page.body
]
}
-}
newLayoutModule : Data -> CodeGen.Module
Expand All @@ -63,33 +68,40 @@ newLayoutModule data =
|> CodeGen.Import.withExposing [ "Html" ]
, CodeGen.Import.new [ "Html", "Attributes" ]
|> CodeGen.Import.withAlias "Attr"
, CodeGen.Import.new [ "View" ]
|> CodeGen.Import.withExposing [ "View" ]
]
, declarations =
[ CodeGen.Declaration.function
{ name = "layout"
, annotation =
CodeGen.Annotation.function
[ CodeGen.Annotation.record
[ ( "page", CodeGen.Annotation.type_ "Html msg" )
[ ( "page", CodeGen.Annotation.type_ "View msg" )
]
, CodeGen.Annotation.type_ "Html msg"
, CodeGen.Annotation.type_ "View msg"
]
, arguments = [ CodeGen.Argument.new "{ page }" ]
, expression =
CodeGen.Expression.multilineFunction
{ name = "Html.div"
, arguments =
[ CodeGen.Expression.list
[ CodeGen.Expression.function
{ name = "Attr.class"
, arguments = [ CodeGen.Expression.string "layout" ]
CodeGen.Expression.multilineRecord
[ ( "title", CodeGen.Expression.value "page.title" )
, ( "body"
, CodeGen.Expression.list
[ CodeGen.Expression.multilineFunction
{ name = "Html.div"
, arguments =
[ CodeGen.Expression.list
[ CodeGen.Expression.function
{ name = "Attr.class"
, arguments = [ CodeGen.Expression.string "layout" ]
}
]
, CodeGen.Expression.value "page.body"
]
}
]
, CodeGen.Expression.list
[ CodeGen.Expression.value "page"
]
]
}
)
]
}
]
}

0 comments on commit 12744d1

Please sign in to comment.