-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d84e6e9
commit a9d0c7f
Showing
17 changed files
with
455 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions
84
packages/design-system/src/components/OcEmojiPicker/OcEmojiPicker.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<template> | ||
<div ref="emojiPickerRef"></div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, nextTick, ref, unref, watch } from 'vue' | ||
import { useGettext } from 'vue3-gettext' | ||
import { Picker } from 'emoji-mart' | ||
/** | ||
* Emoji Picker | ||
*/ | ||
export default defineComponent({ | ||
name: 'OcEmojiPicker', | ||
status: 'ready', | ||
release: '1.0.0', | ||
props: { | ||
theme: { | ||
type: String, | ||
default: 'light', | ||
validator: (value: string) => { | ||
return ['light', 'dark'].includes(value) | ||
} | ||
} | ||
}, | ||
emits: ['emojiSelect', 'clickOutside'], | ||
setup(props, { emit }) { | ||
const { $gettext, current: currentLanguage } = useGettext() | ||
const emojiPickerRef = ref<HTMLElement>() | ||
watch( | ||
[() => props.theme, currentLanguage], | ||
async () => { | ||
await nextTick() | ||
const i18n = { | ||
search: $gettext('Search'), | ||
search_no_results_1: $gettext('Oh no!'), | ||
search_no_results_2: $gettext('That emoji couldn’t be found'), | ||
pick: $gettext('Pick an emoji…'), | ||
add_custom: $gettext('Add custom emoji'), | ||
categories: { | ||
activity: $gettext('Activity'), | ||
custom: $gettext('Custom'), | ||
flags: $gettext('Flags'), | ||
foods: $gettext('Food & Drink'), | ||
frequent: $gettext('Frequently used'), | ||
nature: $gettext('Animals & Nature'), | ||
objects: $gettext('Objects'), | ||
people: $gettext('Smileys & People'), | ||
places: $gettext('Travel & Places'), | ||
search: $gettext('Search Results'), | ||
symbols: $gettext('Symbols') | ||
}, | ||
skins: { | ||
choose: $gettext('Choose default skin tone'), | ||
'1': $gettext('Default'), | ||
'2': $gettext('Light'), | ||
'3': $gettext('Medium-Light'), | ||
'4': $gettext('Medium'), | ||
'5': $gettext('Medium-Dark'), | ||
'6': $gettext('Dark') | ||
} | ||
} | ||
const pickerOptions = { | ||
onEmojiSelect: (emoji: any) => emit('emojiSelect', emoji.native), | ||
onClickOutside: () => emit('clickOutside'), | ||
i18n, | ||
theme: props.theme | ||
} | ||
const picker = new Picker(pickerOptions) | ||
unref(emojiPickerRef).innerHTML = '' | ||
unref(emojiPickerRef).appendChild(picker as any) | ||
}, | ||
{ immediate: true } | ||
) | ||
return { | ||
emojiPickerRef | ||
} | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/web-pkg/src/components/Modals/EmojiPickerModal.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<template> | ||
<oc-emoji-picker :theme="theme" @emoji-select="onEmojiSelect" /> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { computed, defineComponent, onMounted, PropType, unref } from 'vue' | ||
import { Modal, useThemeStore } from '../../composables' | ||
import { storeToRefs } from 'pinia' | ||
export default defineComponent({ | ||
name: 'EmojiPickerModal', | ||
components: {}, | ||
props: { | ||
modal: { type: Object as PropType<Modal>, required: true } | ||
}, | ||
emits: ['confirm'], | ||
setup(props, { emit }) { | ||
const themeStore = useThemeStore() | ||
const { currentTheme } = storeToRefs(themeStore) | ||
const theme = computed(() => { | ||
return unref(currentTheme).isDark ? 'dark' : 'light' | ||
}) | ||
const onEmojiSelect = (emoji: string) => { | ||
emit('confirm', emoji) | ||
} | ||
onMounted(() => { | ||
const modalEl = document.querySelector<HTMLElement>('.oc-modal') | ||
modalEl.style.width = 'auto' | ||
}) | ||
return { | ||
onEmojiSelect, | ||
theme | ||
} | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as ResourceConflictModal } from './ResourceConflictModal.vue' | ||
export { default as SpaceMoveInfoModal } from './SpaceMoveInfoModal.vue' | ||
export { default as EmojiPickerModal } from './EmojiPickerModal.vue' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
packages/web-pkg/src/composables/actions/spaces/useSpaceActionsSetIcon.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { SpaceResource } from '@ownclouders/web-client' | ||
import { computed } from 'vue' | ||
import { SpaceAction, SpaceActionOptions } from '../types' | ||
import { useClientService } from '../../clientService' | ||
import { useLoadingService } from '../../loadingService' | ||
import { useGettext } from 'vue3-gettext' | ||
import { useMessages, useModals, useSpacesStore, useUserStore } from '../../piniaStores' | ||
import { useCreateSpace } from '../../spaces' | ||
import { buildSpace } from '@ownclouders/web-client/src/helpers' | ||
import { eventBus } from '../../../services' | ||
import { Drive } from '@ownclouders/web-client/src/generated' | ||
import { blobToArrayBuffer, canvasToBlob } from '../../../helpers' | ||
import EmojiPickerModal from '../../../components/Modals/EmojiPickerModal.vue' | ||
|
||
export const useSpaceActionsSetIcon = () => { | ||
const userStore = useUserStore() | ||
const { showMessage, showErrorMessage } = useMessages() | ||
const { $gettext } = useGettext() | ||
const clientService = useClientService() | ||
const loadingService = useLoadingService() | ||
const { createDefaultMetaFolder } = useCreateSpace() | ||
const spacesStore = useSpacesStore() | ||
const { dispatchModal } = useModals() | ||
const handler = ({ resources }: SpaceActionOptions) => { | ||
if (resources.length !== 1) { | ||
return | ||
} | ||
|
||
dispatchModal({ | ||
title: $gettext('Set icon for %{space}', { space: resources[0].name }), | ||
hideConfirmButton: true, | ||
customComponent: EmojiPickerModal, | ||
onConfirm: (emoji: string) => setIconSpace(resources[0], emoji) | ||
}) | ||
} | ||
|
||
const generateEmojiImage = async (emoji: string): Promise<ArrayBuffer | string> => { | ||
const canvas = document.createElement('canvas') | ||
const context = canvas.getContext('2d') | ||
const aspectRatio = 16 / 9, | ||
width = 720, | ||
height = width / aspectRatio | ||
|
||
canvas.width = width | ||
canvas.height = height | ||
|
||
const textSize = 0.4 * width | ||
context.font = `${textSize}px sans-serif` | ||
|
||
context.textBaseline = 'middle' | ||
context.textAlign = 'center' | ||
|
||
context.fillText(emoji, canvas.width / 2, canvas.height / 2) | ||
|
||
const blob = await canvasToBlob(canvas) | ||
return blobToArrayBuffer(blob) | ||
} | ||
|
||
const setIconSpace = async (space: SpaceResource, emoji: string) => { | ||
const graphClient = clientService.graphAuthenticated | ||
const content = await generateEmojiImage(emoji) | ||
|
||
try { | ||
await clientService.webdav.getFileInfo(space, { path: '.space' }) | ||
} catch (_) { | ||
spacesStore.updateSpaceField({ | ||
id: space.id, | ||
field: 'spaceReadmeData', | ||
value: (await createDefaultMetaFolder(space)).spaceReadmeData | ||
}) | ||
} | ||
|
||
return loadingService.addTask(async () => { | ||
const headers = { | ||
'Content-Type': 'application/offset+octet-stream' | ||
} | ||
|
||
try { | ||
const { fileId } = await clientService.webdav.putFileContents(space, { | ||
path: `/.space/emoji.png`, | ||
content, | ||
headers, | ||
overwrite: true | ||
}) | ||
|
||
const { data } = await graphClient.drives.updateDrive( | ||
space.id.toString(), | ||
{ | ||
special: [ | ||
{ | ||
specialFolder: { | ||
name: 'image' | ||
}, | ||
id: fileId | ||
} | ||
] | ||
} as Drive, | ||
{} | ||
) | ||
|
||
spacesStore.updateSpaceField({ | ||
id: space.id.toString(), | ||
field: 'spaceImageData', | ||
value: data.special.find((special) => special.specialFolder.name === 'image') | ||
}) | ||
showMessage({ title: $gettext('Space icon was set successfully') }) | ||
eventBus.publish('app.files.spaces.uploaded-image', buildSpace(data)) | ||
} catch (error) { | ||
console.error(error) | ||
showErrorMessage({ | ||
title: $gettext('Failed to set space icon'), | ||
errors: [error] | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
const actions = computed((): SpaceAction[] => [ | ||
{ | ||
name: 'set-space-icon', | ||
icon: 'emoji-sticker', | ||
handler, | ||
label: () => { | ||
return $gettext('Set icon') | ||
}, | ||
isVisible: ({ resources }) => { | ||
if (resources.length !== 1) { | ||
return false | ||
} | ||
|
||
return resources[0].canEditImage({ user: userStore.user }) | ||
}, | ||
componentType: 'button', | ||
class: 'oc-files-actions-set-space-icon-trigger' | ||
} | ||
]) | ||
|
||
return { | ||
actions, | ||
setIconSpace | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const blobToArrayBuffer: (blob: Blob) => Promise<string | ArrayBuffer> = (blob: Blob) => { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader() | ||
reader.onloadend = () => resolve(reader.result) | ||
reader.onerror = (e) => reject(e) | ||
reader.readAsArrayBuffer(blob) | ||
}) | ||
} | ||
|
||
export const canvasToBlob = (canvas: HTMLCanvasElement): Promise<Blob> => { | ||
return new Promise((resolve) => canvas.toBlob(resolve)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.