ident
All repositories: gitoria
8.0 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). Host assignments reset the starting values when the form opens.// 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 = trueinput.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() != '' }}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
- 0f6d1592Merge branch 'f18b-shell-session' into mainIdent
- 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
- 5d1b3fedMerge branch 'v61-day61' into mainIdent
- 83faeb15|||PR ident: vendor Hybriel day61mre
- b2572b34Merge branch 'v59-day59' into mainIdent
- 67cf016d|||PR ident: vendor Hybriel day59mre
- 600c043dMerge branch 'v58-day58' into mainIdent
- 365f8c80|||PR ident: vendor Hybriel day58mre
- 585126e5Merge branch 'f16-setf' into mainIdent
- 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
- a1419d9cMerge branch 'f15-navigate' into mainIdent
- 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
- e36f4516Merge branch 'f14-online' into mainIdent
- 2c5eac91|||PR ident#38 online state without probe timermre
- bf1980f2Merge branch 'v56-day56' into mainIdent
- 62ae0adf|||PR ident: vendor Hybriel day56mre
- 0d18abc4Merge branch 'f13-avatar' into mainIdent
- 2a99730a|||PR ident#37 avatar in hlmre
- fa4ed25bMerge branch 'f11-upsert' into mainIdent
- 8f3d9bc9|||PR ident#42 keyed upsert and removemre