ident
All repositories: gitoria
8.1 KB
// components/identityform.hl — THE IDENTITY FORM (ident#40.3), placed by components/home.hl while it is open: a new identity, an edit,// and right after the first login the default identity ("welcome": the names are optional, Skip closes it). The host decides what// the form shows and passes it in: the heading, whether it is the welcome form, which identity (editId, or welcomeId for the welcome// form; '' = a new one) and the starting values (the f… fields). This component owns the typing, the avatar picture and the save// (faces addIdentity and editIdentity, lib/identities.hl). Every typed value is also handed up ($parent.setF), so the host's copy// stays the one it resets when it opens the form for another identity. The host shows the messages: $parent.status(message, notice);// $parent.formSaved(rows, text) takes the new list after a create (a new identity changes the other rows: the first gets its Delete button, and the order is the server's), $parent.rowSaved(row, text) the one edited row, $parent.closeForm() closes the form (Skip, Cancel).import { accountOfSession } from '../lib/accounts.hl'import { identityRows, identityRow, createIdentity, updateIdentity } from '../lib/identities.hl'import { leadLook } from './styles.hl'editHeading = ''welcome = falsenotWelcome = !welcome // `if (!welcome)` in a View renders nothing: an `if` takes a membereditId = ''welcomeId = ''fIdentityName = ''fNickname = ''fFirstname = ''fLastname = ''fAvatar = ''avatarNote = ''hasAvatarPreview = false// the preview and the Remove button are hidden (the attribute) while there is no picturenoAvatarPreview = !hasAvatarPreviewStyle {pLead = leadLookavatarField {display = 'flex'flexWrap = 'wrap'alignItems = 'center'gap = '.6rem'// real Chrome: a file input has an intrinsic width (its "No file chosen" text) that pushed the form to 413 px at 390: every item may shrinklabel {flex = '1 1 12rem'minWidth = '0'}}'#avatarpreview' {width = '48px'height = '48px'objectFit = 'cover'borderRadius = '50%'background = 'var(--color-background-sunken)'}'#identityform input[type=file]' {width = '100%'maxWidth = '100%'minWidth = '0'}'#avatarnote' {flexBasis = '100%'color = 'var(--color-danger)'fontSize = '.875rem'empty { display = 'none' }}}View {form { id = "identityform"on submit(e) {e.preventDefault()emit saveForm(e)}h1 { id = "formheading" editHeading }if (welcome) {p { id = "welcome" Style.pLead "Your account is ready and has a default identity. All of these fields are " strong { "optional" } " — fill in what you like, or skip it and do it later." }}label { "Identity name " fieldHint { "(shown when you pick an identity in an app; optional)" }input { id = "fidentityname" name = "identityName" maxlength = "60" autocomplete = "off" value = fIdentityName on input(e) { emit setF('identityName', e.target.value) } }}label { "Nickname " fieldHint { "(optional)" }input { id = "fnickname" name = "nickname" maxlength = "60" autocomplete = "nickname" value = fNickname on input(e) { emit setF('nickname', e.target.value) } }}label { "First name " fieldHint { "(optional)" }input { id = "ffirstname" name = "firstname" maxlength = "60" autocomplete = "given-name" value = fFirstname on input(e) { emit setF('firstname', e.target.value) } }}label { "Last name " fieldHint { "(optional)" }input { id = "flastname" name = "lastname" maxlength = "60" autocomplete = "family-name" value = fLastname on input(e) { emit setF('lastname', e.target.value) } }}avatarField {img { id = "avatarpreview" hidden = noAvatarPreview src = fAvatar alt = "" width = "48" height = "48" }label { "Avatar " fieldHint { "(optional; a picture apps can show, e.g. next to your comments)" }input { id = "favatarfile" type = "file" accept = "image/png,image/jpeg,image/webp,image/gif" on change(Hybrid e) { pickAvatar(e) } }}input { id = "favatar" name = "avatar" type = "hidden" value = fAvatar on input(Hybrid e) { emit setF('avatar', e.target.value) } }button { id = "avatarremove" type = "button" class = "quiet small" hidden = noAvatarPreview "Remove picture" on click(e) { emit setF('avatar', '') } }span { id = "avatarnote" avatarNote }}formButtons {button { id = "saveidentity" type = "submit" "Save" }if (welcome) { button { id = "skip" type = "button" class = "quiet" "Skip" on click(e) { $parent.closeForm() } } }if (notWelcome) { button { id = "cancel" type = "button" class = "quiet" "Cancel" on click(e) { $parent.closeForm() } } }}}}// Browser constructors are documented in COMPONENTS §6b. Only members carry form state.pickAvatar(Hybrid e) {input = e.targetfile = input.files[0]if (file == null) { return null }avatarNote = ''if (!['image/png', 'image/jpeg', 'image/webp', 'image/gif'].includes(file.type)) {avatarNote = 'That is not a picture (PNG, JPEG, WebP or GIF).'input.value = ''return null}if (file.size > 20 * 1024 * 1024) {avatarNote = 'That picture is too big (at most 20 MB).'input.value = ''return null}src = URL.createObjectURL(file)image = new Image()image.onerror = (Hybrid event) => {URL.revokeObjectURL(src)avatarNote = 'That picture could not be read.'input.value = ''}image.onload = (Hybrid event) => {URL.revokeObjectURL(src)side = Math.min(image.naturalWidth, image.naturalHeight)if (!side) { avatarNote = 'That picture could not be read.' return null }canvas = new OffscreenCanvas(128, 128)cx = canvas.getContext('2d')cx.drawImage(image, (image.naturalWidth - side) / 2, (image.naturalHeight - side) / 2, side, side, 0, 0, 128, 128)blob = canvas.convertToBlob({ type = 'image/webp' quality = 0.85 })reader = new FileReader()reader.onload = (Hybrid event) => {url = event.target.resultif (!url.startsWith('data:image/webp') && url.length > 60000) {fallback = canvas.convertToBlob({ type = 'image/jpeg' quality = 0.85 })jpegReader = new FileReader()jpegReader.onload = (Hybrid event) => { acceptAvatar(event.target.result, input) }jpegReader.readAsDataURL(fallback)return null}acceptAvatar(url, input)}reader.readAsDataURL(blob)}image.src = src}acceptAvatar(String url, Hybrid input) {if (url.length > 60000) {avatarNote = 'That picture is too complex — try another one.'return null}fAvatar = urlhasAvatarPreview = true$parent.setF('avatar', url)input.value = ''}on setF(name, v) {if (name == 'identityName') { fIdentityName = v }if (name == 'nickname') { fNickname = v }if (name == 'firstname') { fFirstname = v }if (name == 'lastname') { fLastname = v }if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }$parent.setF(name, v)}on saveForm(e) {$parent.status('', '')fields = { identityName = fIdentityName nickname = fNickname firstname = fFirstname lastname = fLastname avatar = fAvatar }let r = nullif (editId == '' && welcome) {r = emit server editIdentity('' + welcomeId, fields)} else if (editId == '') {r = emit server addIdentity(fields)} else {r = emit server editIdentity('' + editId, fields)}if (r == null) {$parent.status('the server did not answer — try again', '')return null}if (r.error != null) {$parent.status(r.error, '')return null}if (r.row != null) {$parent.rowSaved(r.row, 'Identity saved.')return null}$parent.formSaved(r.identities, 'Identity created.')}// A face runs on a BLANK instance (its arguments and its imports only); the account comes from the session.on server addIdentity(fields, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = createIdentity(me.id, fields)if (r.error != null) { return r }return { identity = r.identity identities = identityRows(me.id) }}on server editIdentity(id, fields, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = updateIdentity(me.id, id, fields)if (r.error != null) { return r }return { identity = r.identity row = identityRow(me.id, r.identity) }}
Branches
Latest commits
- 2a99730a|||PR ident#37 avatar in hlmre
- fa4ed25bMerge branch 'f11-upsert' into mainIdent
- 8f3d9bc9|||PR ident#42 keyed upsert and removemre
- 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
- 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
- 991641d4Merge branch 'f10-access' into mainIdent
- e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
- df7696dbident#34: tests: access ticks selector quoting fixmre
- 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
- 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
- dc702419Merge branch 'f9-choose' into mainIdent
- 8caec771|||PR ident#34 restore .choose hookmre
- 0dbff960Merge branch 'f8-shared' into mainIdent
- d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
- 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
- eb039073ident#34: tests: signin and migration find the choice without the classmre
- 4b94a594ident#34: tests: fix selector of the identity choicemre
- bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
- b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
- 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre