提交 07c9f2a3 编写于 作者: fxy060608's avatar fxy060608

feat(i18n): refactor

上级 3f6eac4a
const path = require('path') const path = require('path')
const json = require('@rollup/plugin-json')
const alias = require('@rollup/plugin-alias') const alias = require('@rollup/plugin-alias')
const replace = require('@rollup/plugin-replace') const replace = require('@rollup/plugin-replace')
...@@ -71,7 +72,8 @@ module.exports = { ...@@ -71,7 +72,8 @@ module.exports = {
find: 'uni-helpers', find: 'uni-helpers',
replacement: path.resolve(__dirname, '../src/core/helpers') replacement: path.resolve(__dirname, '../src/core/helpers')
}] }]
}), }),
json(),
replace({ replace({
__GLOBAL__: platform.prefix, __GLOBAL__: platform.prefix,
__PLATFORM_TITLE__: platform.title, __PLATFORM_TITLE__: platform.title,
......
const path = require('path') const path = require('path')
const json = require('@rollup/plugin-json')
const alias = require('@rollup/plugin-alias') const alias = require('@rollup/plugin-alias')
const replace = require('@rollup/plugin-replace') const replace = require('@rollup/plugin-replace')
const nodeResolve = require('@rollup/plugin-node-resolve') const nodeResolve = require('@rollup/plugin-node-resolve')
...@@ -56,7 +57,8 @@ alias({ ...@@ -56,7 +57,8 @@ alias({
find: 'uni-api-protocol', find: 'uni-api-protocol',
replacement: resolve('src/core/helpers/protocol') replacement: resolve('src/core/helpers/protocol')
}] }]
}), }),
json(),
nodeResolve(), nodeResolve(),
requireContext(), requireContext(),
commonjs(), commonjs(),
......
const path = require('path') const path = require('path')
const json = require('@rollup/plugin-json')
const alias = require('@rollup/plugin-alias') const alias = require('@rollup/plugin-alias')
const replace = require('@rollup/plugin-replace') const replace = require('@rollup/plugin-replace')
...@@ -23,7 +24,8 @@ module.exports = { ...@@ -23,7 +24,8 @@ module.exports = {
replacement: path.resolve(__dirname, '../src/core/helpers') replacement: path.resolve(__dirname, '../src/core/helpers')
} }
] ]
}), }),
json(),
replace({ replace({
__GLOBAL__: 'getGlobalUni()', __GLOBAL__: 'getGlobalUni()',
__PLATFORM_TITLE__: 'app-plus-nvue' __PLATFORM_TITLE__: 'app-plus-nvue'
......
...@@ -2272,10 +2272,6 @@ var serviceContext = (function () { ...@@ -2272,10 +2272,6 @@ var serviceContext = (function () {
return compiled; return compiled;
} }
let curLocale = 'en';
let fallbackLocale = 'en';
let curMessages = {};
let messages = {};
const hasOwnProperty$1 = Object.prototype.hasOwnProperty; const hasOwnProperty$1 = Object.prototype.hasOwnProperty;
const hasOwn$1 = (val, key) => hasOwnProperty$1.call(val, key); const hasOwn$1 = (val, key) => hasOwnProperty$1.call(val, key);
const defaultFormatter = new BaseFormatter(); const defaultFormatter = new BaseFormatter();
...@@ -2285,9 +2281,9 @@ var serviceContext = (function () { ...@@ -2285,9 +2281,9 @@ var serviceContext = (function () {
function startsWith(str, parts) { function startsWith(str, parts) {
return parts.find((part) => str.indexOf(part) === 0); return parts.find((part) => str.indexOf(part) === 0);
} }
function normalizeLocale(locale) { function normalizeLocale(locale, messages) {
if (!locale) { if (!locale) {
return fallbackLocale; return;
} }
locale = locale.trim().replace(/_/g, '-'); locale = locale.trim().replace(/_/g, '-');
if (messages[locale]) { if (messages[locale]) {
...@@ -2310,31 +2306,135 @@ var serviceContext = (function () { ...@@ -2310,31 +2306,135 @@ var serviceContext = (function () {
if (lang) { if (lang) {
return lang; return lang;
} }
return fallbackLocale;
} }
var index = { class I18n {
init(options) { constructor({ locale, fallbackLocale, messages, watcher, formater, }) {
if (options.fallbackLocale) { this.locale = 'en';
fallbackLocale = options.fallbackLocale; this.fallbackLocale = 'en';
this.message = {};
this.messages = {};
this.watchers = [];
if (fallbackLocale) {
this.fallbackLocale = fallbackLocale;
} }
messages = options.messages; this.formater = formater || defaultFormatter;
this.setLocale(options.locale); this.messages = messages;
}, this.setLocale(locale);
if (watcher) {
this.watchLocale(watcher);
}
}
setLocale(locale) { setLocale(locale) {
curLocale = normalizeLocale(locale); const oldLocale = this.locale;
curMessages = messages[curLocale]; this.locale = normalizeLocale(locale, this.messages) || this.fallbackLocale;
}, this.message = this.messages[this.locale];
this.watchers.forEach((watcher) => {
watcher(this.locale, oldLocale);
});
}
getLocale() { getLocale() {
return curLocale; return this.locale;
}, }
watchLocale(fn) {
const index = this.watchers.push(fn) - 1;
return () => {
this.watchers.splice(index, 1);
};
}
t(key, values) { t(key, values) {
if (!hasOwn$1(curMessages, key)) { if (!hasOwn$1(this.message, key)) {
console.warn(`Cannot translate the value of keypath ${key}. Use the value of keypath as default.`); console.warn(`Cannot translate the value of keypath ${key}. Use the value of keypath as default.`);
return key; return key;
} }
return defaultFormatter.interpolate(curMessages[key], values).join(''); return this.formater.interpolate(this.message[key], values).join('');
}, }
}; }
function initLocaleWatcher(appVm, i18n) {
appVm.$i18n &&
appVm.$i18n.vm.$watch('locale', (newLocale) => {
i18n.setLocale(newLocale);
}, {
immediate: true,
});
}
function getDefaultLocale() {
if (typeof navigator !== 'undefined') {
return navigator.userLanguage || navigator.language;
}
if (typeof plus !== 'undefined') {
// TODO 待调整为最新的获取语言代码
return plus.os.language;
}
return uni.getSystemInfoSync().language;
}
function initVueI18n(messages, fallbackLocale = 'en', locale) {
const i18n = new I18n({
locale: locale || fallbackLocale,
fallbackLocale,
messages,
});
let t = (key, values) => {
if (typeof getApp !== 'function') {
// app-plus view
/* eslint-disable no-func-assign */
t = function (key, values) {
return i18n.t(key, values);
};
}
else {
const appVm = getApp().$vm;
if (!appVm.$t || !appVm.$i18n) {
if (!locale) {
i18n.setLocale(getDefaultLocale());
}
/* eslint-disable no-func-assign */
t = function (key, values) {
return i18n.t(key, values);
};
}
else {
initLocaleWatcher(appVm, i18n);
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n;
const silentTranslationWarn = $i18n.silentTranslationWarn;
$i18n.silentTranslationWarn = true;
const msg = appVm.$t(key, values);
$i18n.silentTranslationWarn = silentTranslationWarn;
if (msg !== key) {
return msg;
}
return i18n.t(key, values);
};
}
}
return t(key, values);
};
return {
t(key, values) {
return t(key, values);
},
setLocale(newLocale) {
return i18n.setLocale(newLocale);
},
mixin: {
beforeCreate() {
const unwatch = i18n.watchLocale(() => {
this.$forceUpdate();
});
this.$once('hook:beforeDestroy', function () {
unwatch();
});
},
methods: {
$$t(key, values) {
return t(key, values);
},
},
},
};
}
var en = { var en = {
"uni.showActionSheet.cancel": "cancel", "uni.showActionSheet.cancel": "cancel",
...@@ -2386,7 +2486,7 @@ var serviceContext = (function () { ...@@ -2386,7 +2486,7 @@ var serviceContext = (function () {
"uni.button.feedback.send": "發送" "uni.button.feedback.send": "發送"
}; };
const messages$1 = { const messages = {
en, en,
es, es,
fr, fr,
...@@ -2394,56 +2494,10 @@ var serviceContext = (function () { ...@@ -2394,56 +2494,10 @@ var serviceContext = (function () {
'zh-Hant': zhHant 'zh-Hant': zhHant
}; };
const fallbackLocale$1 = 'en'; const fallbackLocale = 'en';
function initI18n (locale, onChange) { const i18n = initVueI18n( messages , fallbackLocale);
index.init({ const t = i18n.t;
locale,
fallbackLocale: fallbackLocale$1,
messages: messages$1
});
if (onChange) {
index.watchLocale((newLocale, oldLocale) => {
onChange(newLocale, oldLocale);
});
}
}
function initLocaleWatcher (appVm) {
appVm.$i18n.vm.$watch('locale', (newLocale) => {
index.setLocale(newLocale);
}, {
immediate: true
});
}
function t (key, values) {
if (__VIEW__) {
return index.t(key, values)
}
const appVm = getApp().$vm;
if (!appVm.$t) {
/* eslint-disable no-func-assign */
t = function (key, values) {
return index.t(key, values)
};
} else {
initLocaleWatcher(appVm);
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n;
const silentTranslationWarn = $i18n.silentTranslationWarn;
$i18n.silentTranslationWarn = true;
const msg = appVm.$t(key, values);
$i18n.silentTranslationWarn = silentTranslationWarn;
if (msg !== key) {
return msg
}
return index.t(key, values)
};
}
return t(key, values)
}
const showModal = { const showModal = {
title: { title: {
...@@ -5820,12 +5874,12 @@ var serviceContext = (function () { ...@@ -5820,12 +5874,12 @@ var serviceContext = (function () {
}, fail); }, fail);
} }
let index$1 = 0; let index = 0;
function saveFile$1 ({ function saveFile$1 ({
tempFilePath tempFilePath
} = {}, callbackId) { } = {}, callbackId) {
const errorCallback = warpPlusErrorCallback(callbackId, 'saveFile'); const errorCallback = warpPlusErrorCallback(callbackId, 'saveFile');
const fileName = `${Date.now()}${index$1++}${getExtName(tempFilePath)}`; const fileName = `${Date.now()}${index++}${getExtName(tempFilePath)}`;
plus.io.resolveLocalFileSystemURL(tempFilePath, entry => { // 读取临时文件 FileEntry plus.io.resolveLocalFileSystemURL(tempFilePath, entry => { // 读取临时文件 FileEntry
getSavedFileDir(dir => { getSavedFileDir(dir => {
...@@ -12323,7 +12377,7 @@ var serviceContext = (function () { ...@@ -12323,7 +12377,7 @@ var serviceContext = (function () {
// misrepresented as being the original software. // misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution. // 3. This notice may not be removed or altered from any source distribution.
var messages$2 = { var messages$1 = {
2: 'need dictionary', /* Z_NEED_DICT 2 */ 2: 'need dictionary', /* Z_NEED_DICT 2 */
1: 'stream end', /* Z_STREAM_END 1 */ 1: 'stream end', /* Z_STREAM_END 1 */
0: '', /* Z_OK 0 */ 0: '', /* Z_OK 0 */
...@@ -12458,7 +12512,7 @@ var serviceContext = (function () { ...@@ -12458,7 +12512,7 @@ var serviceContext = (function () {
var OS_CODE = 0x03; // Unix :) . Don't detect, use this default. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
function err(strm, errorCode) { function err(strm, errorCode) {
strm.msg = messages$2[errorCode]; strm.msg = messages$1[errorCode];
return errorCode; return errorCode;
} }
...@@ -14606,7 +14660,7 @@ var serviceContext = (function () { ...@@ -14606,7 +14660,7 @@ var serviceContext = (function () {
); );
if (status !== Z_OK$1) { if (status !== Z_OK$1) {
throw new Error(messages$2[status]); throw new Error(messages$1[status]);
} }
if (opt.header) { if (opt.header) {
...@@ -14628,7 +14682,7 @@ var serviceContext = (function () { ...@@ -14628,7 +14682,7 @@ var serviceContext = (function () {
status = deflate_1.deflateSetDictionary(this.strm, dict); status = deflate_1.deflateSetDictionary(this.strm, dict);
if (status !== Z_OK$1) { if (status !== Z_OK$1) {
throw new Error(messages$2[status]); throw new Error(messages$1[status]);
} }
this._dict_set = true; this._dict_set = true;
...@@ -14806,7 +14860,7 @@ var serviceContext = (function () { ...@@ -14806,7 +14860,7 @@ var serviceContext = (function () {
deflator.push(input, true); deflator.push(input, true);
// That will never happens, if you don't cheat with options :) // That will never happens, if you don't cheat with options :)
if (deflator.err) { throw deflator.msg || messages$2[deflator.err]; } if (deflator.err) { throw deflator.msg || messages$1[deflator.err]; }
return deflator.result; return deflator.result;
} }
...@@ -17358,7 +17412,7 @@ var serviceContext = (function () { ...@@ -17358,7 +17412,7 @@ var serviceContext = (function () {
); );
if (status !== constants.Z_OK) { if (status !== constants.Z_OK) {
throw new Error(messages$2[status]); throw new Error(messages$1[status]);
} }
this.header = new gzheader(); this.header = new gzheader();
...@@ -17376,7 +17430,7 @@ var serviceContext = (function () { ...@@ -17376,7 +17430,7 @@ var serviceContext = (function () {
if (opt.raw) { //In raw mode we need to set the dictionary early if (opt.raw) { //In raw mode we need to set the dictionary early
status = inflate_1.inflateSetDictionary(this.strm, opt.dictionary); status = inflate_1.inflateSetDictionary(this.strm, opt.dictionary);
if (status !== constants.Z_OK) { if (status !== constants.Z_OK) {
throw new Error(messages$2[status]); throw new Error(messages$1[status]);
} }
} }
} }
...@@ -17606,7 +17660,7 @@ var serviceContext = (function () { ...@@ -17606,7 +17660,7 @@ var serviceContext = (function () {
inflator.push(input, true); inflator.push(input, true);
// That will never happens, if you don't cheat with options :) // That will never happens, if you don't cheat with options :)
if (inflator.err) { throw inflator.msg || messages$2[inflator.err]; } if (inflator.err) { throw inflator.msg || messages$1[inflator.err]; }
return inflator.result; return inflator.result;
} }
...@@ -20765,7 +20819,6 @@ var serviceContext = (function () { ...@@ -20765,7 +20819,6 @@ var serviceContext = (function () {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.log('[uni-app] registerApp'); console.log('[uni-app] registerApp');
} }
appVm.$$t = t;
appCtx = appVm; appCtx = appVm;
appCtx.$vm = appVm; appCtx.$vm = appVm;
...@@ -20775,9 +20828,6 @@ var serviceContext = (function () { ...@@ -20775,9 +20828,6 @@ var serviceContext = (function () {
// merge globalData // merge globalData
appCtx.globalData = Object.assign(globalData, appCtx.globalData); appCtx.globalData = Object.assign(globalData, appCtx.globalData);
// TODO
initI18n(plus.os.language);
initOn(UniServiceJSBridge.on, { initOn(UniServiceJSBridge.on, {
getApp: getApp$1, getApp: getApp$1,
getCurrentPages: getCurrentPages$1 getCurrentPages: getCurrentPages$1
...@@ -21746,7 +21796,7 @@ var serviceContext = (function () { ...@@ -21746,7 +21796,7 @@ var serviceContext = (function () {
UniServiceJSBridge.invokeCallbackHandler = invokeCallbackHandler; UniServiceJSBridge.invokeCallbackHandler = invokeCallbackHandler;
UniServiceJSBridge.removeCallbackHandler = removeCallbackHandler; UniServiceJSBridge.removeCallbackHandler = removeCallbackHandler;
var index$2 = { var index$1 = {
__vuePlugin: vuePlugin, __vuePlugin: vuePlugin,
__definePage: definePage, __definePage: definePage,
__registerApp: registerApp, __registerApp: registerApp,
...@@ -21756,7 +21806,7 @@ var serviceContext = (function () { ...@@ -21756,7 +21806,7 @@ var serviceContext = (function () {
getCurrentPages: getCurrentPages$1 getCurrentPages: getCurrentPages$1
}; };
return index$2; return index$1;
}()); }());
......
import en from './en.json'
import es from './es.json'
import fr from './fr.json'
import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.json'
export default {
en,
es,
fr,
'zh-Hans': zhHans,
'zh-Hant': zhHant
}
...@@ -12,28 +12,10 @@ ...@@ -12,28 +12,10 @@
</template> </template>
<script> <script>
import defaultMessage from './i18n/zh-Hans.json' import { initVueI18n } from '@dcloudio/uni-i18n'
function createTranslate (defaultMessage) { import messages from './i18n/index'
let t = (key, values) => {
const appVm = getApp().$vm const { t } = initVueI18n(messages)
if (appVm && appVm.$t) {
t = (key, values) => {
const msg = appVm.$t(key, values)
if (msg !== key) {
return msg
}
return defaultMessage[key]
}
} else {
t = (key, values) => {
return defaultMessage[key]
}
}
return t(key, values)
}
return t
}
const t = createTranslate(defaultMessage)
const events = { const events = {
load: 'load', load: 'load',
......
...@@ -907,11 +907,6 @@ const protocols = { // 需要做转换的 API 列表 ...@@ -907,11 +907,6 @@ const protocols = { // 需要做转换的 API 列表
text: 'data' text: 'data'
} }
}, },
pageScrollTo: {
args: {
duration: false
}
},
login: { login: {
name: 'getAuthCode', name: 'getAuthCode',
returnValue (result) { returnValue (result) {
...@@ -1011,6 +1006,7 @@ const protocols = { // 需要做转换的 API 列表 ...@@ -1011,6 +1006,7 @@ const protocols = { // 需要做转换的 API 列表
result.userName = info.fullname; result.userName = info.fullname;
result.provinceName = info.prov; result.provinceName = info.prov;
result.cityName = info.city; result.cityName = info.city;
result.countyName = info.area;
result.detailInfo = info.address; result.detailInfo = info.address;
result.telNumber = info.mobilePhone; result.telNumber = info.mobilePhone;
result.errMsg = result.resultStatus; result.errMsg = result.resultStatus;
......
import i18n from '@dcloudio/uni-i18n' import {
initVueI18n
} from '@dcloudio/uni-i18n'
import en from './en.json' import en from './en.json'
import es from './es.json' import es from './es.json'
...@@ -16,51 +18,7 @@ const messages = { ...@@ -16,51 +18,7 @@ const messages = {
const fallbackLocale = 'en' const fallbackLocale = 'en'
export function initI18n (locale, onChange) { const i18n = initVueI18n(__PLATFORM__ === 'app-plus' || __PLATFORM__ === 'h5' ? messages : {}, fallbackLocale)
i18n.init({ export const t = i18n.t
locale, export const i18nMixin = i18n.mixin
fallbackLocale, export const setLocale = i18n.setLocale
messages
})
if (onChange) {
i18n.watchLocale((newLocale, oldLocale) => {
onChange(newLocale, oldLocale)
})
}
}
function initLocaleWatcher (appVm) {
appVm.$i18n.vm.$watch('locale', (newLocale) => {
i18n.setLocale(newLocale)
}, {
immediate: true
})
}
export function t (key, values) {
if (__VIEW__) {
return i18n.t(key, values)
}
const appVm = getApp().$vm
if (!appVm.$t) {
/* eslint-disable no-func-assign */
t = function (key, values) {
return i18n.t(key, values)
}
} else {
initLocaleWatcher(appVm)
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n
const silentTranslationWarn = $i18n.silentTranslationWarn
$i18n.silentTranslationWarn = true
const msg = appVm.$t(key, values)
$i18n.silentTranslationWarn = silentTranslationWarn
if (msg !== key) {
return msg
}
return i18n.t(key, values)
}
}
return t(key, values)
}
import {
t,
initI18n
} from 'uni-core/helpers/i18n'
import initRouterGuard from './router-guard' import initRouterGuard from './router-guard'
let appVm = false let appVm = false
...@@ -69,13 +64,10 @@ export function getCurrentPages (isAll = false, ignoreError = false) { ...@@ -69,13 +64,10 @@ export function getCurrentPages (isAll = false, ignoreError = false) {
} }
export default function createApp (vm, routes) { export default function createApp (vm, routes) {
appVm = vm appVm = vm
appVm.$vm = vm appVm.$vm = vm
appVm.$$t = t
appVm.globalData = appVm.$options.globalData || {} appVm.globalData = appVm.$options.globalData || {}
// h5
initI18n(navigator.userLanguage || navigator.language)
// initEvents(appVm) // initEvents(appVm)
initRouterGuard(appVm, routes) initRouterGuard(appVm, routes)
} }
import i18n from '@dcloudio/uni-i18n'
import {
t
} from 'uni-core/helpers/i18n'
export default {
beforeCreate () {
const unwatch = i18n.watchLocale(() => {
this.$forceUpdate()
})
this.$once('hook:beforeDestroy', function () {
unwatch()
})
},
methods: {
$$t (key, values) {
return t(key, values)
}
}
}
import {
t,
initI18n
} from 'uni-core/helpers/i18n'
import { import {
callAppHook callAppHook
} from 'uni-core/service/plugins/util' } from 'uni-core/service/plugins/util'
...@@ -210,7 +206,6 @@ export function registerApp (appVm) { ...@@ -210,7 +206,6 @@ export function registerApp (appVm) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.log('[uni-app] registerApp') console.log('[uni-app] registerApp')
} }
appVm.$$t = t
appCtx = appVm appCtx = appVm
appCtx.$vm = appVm appCtx.$vm = appVm
...@@ -220,9 +215,6 @@ export function registerApp (appVm) { ...@@ -220,9 +215,6 @@ export function registerApp (appVm) {
// merge globalData // merge globalData
appCtx.globalData = Object.assign(globalData, appCtx.globalData) appCtx.globalData = Object.assign(globalData, appCtx.globalData)
// TODO
initI18n(plus.os.language)
initOn(UniServiceJSBridge.on, { initOn(UniServiceJSBridge.on, {
getApp, getApp,
getCurrentPages getCurrentPages
......
...@@ -3,7 +3,7 @@ import { ...@@ -3,7 +3,7 @@ import {
} from 'uni-shared' } from 'uni-shared'
import { import {
initI18n setLocale
} from 'uni-core/helpers/i18n' } from 'uni-core/helpers/i18n'
import { import {
...@@ -57,7 +57,7 @@ function onPageCreate ({ ...@@ -57,7 +57,7 @@ function onPageCreate ({
onPageReachBottom, onPageReachBottom,
onReachBottomDistance onReachBottomDistance
}, pageId) { }, pageId) {
initI18n(locale) setLocale(locale)
onCssVar({ onCssVar({
statusbarHeight, statusbarHeight,
......
<template> <template>
<uni-actionsheet @touchmove.prevent> <uni-actionsheet @touchmove.prevent>
<transition name="uni-fade"> <transition name="uni-fade">
<div <div
v-show="visible" v-show="visible"
class="uni-mask uni-actionsheet__mask" class="uni-mask uni-actionsheet__mask"
@click="_close(-1)" @click="_close(-1)"
/> />
</transition> </transition>
<div <div
:class="{ 'uni-actionsheet_toggle': visible }" :class="{ 'uni-actionsheet_toggle': visible }"
:style="popupStyle.content" :style="popupStyle.content"
class="uni-actionsheet" class="uni-actionsheet"
> >
<div <div
ref="main" ref="main"
class="uni-actionsheet__menu" class="uni-actionsheet__menu"
@wheel="_handleWheel" @wheel="_handleWheel"
> >
<!-- title占位 --> <!-- title占位 -->
<div <div
v-if="title" v-if="title"
class="uni-actionsheet__cell" class="uni-actionsheet__cell"
:style="{height:`${titleHeight}px`}" :style="{height:`${titleHeight}px`}"
/> />
<div <div
v-if="title" v-if="title"
class="uni-actionsheet__title" class="uni-actionsheet__title"
> >
{{ title }} {{ title }}
</div> </div>
<div <div :style="{maxHeight:`${HEIGHT}px`,overflow:'hidden'}">
:style="{maxHeight:`${HEIGHT}px`,overflow:'hidden'}" <div ref="content">
> <div
<div v-for="(itemTitle, index) in itemList"
ref="content" :key="index"
> :style="{ color: itemColor }"
<div
v-for="(itemTitle, index) in itemList"
:key="index"
:style="{ color: itemColor }"
class="uni-actionsheet__cell" class="uni-actionsheet__cell"
@click="_close(index)" @click="_close(index)"
> >
{{ itemTitle }} {{ itemTitle }}
</div> </div>
...@@ -48,26 +44,28 @@ ...@@ -48,26 +44,28 @@
</div> </div>
</div> </div>
<div class="uni-actionsheet__action"> <div class="uni-actionsheet__action">
<div <div
:style="{ color: itemColor }" :style="{ color: itemColor }"
class="uni-actionsheet__cell" class="uni-actionsheet__cell"
@click="_close(-1)" @click="_close(-1)"
> >
{{ $$t('uni.showActionSheet.cancel') }} {{ $$t('uni.showActionSheet.cancel') }}
</div> </div>
</div> </div>
<div :style="popupStyle.triangle" /> <div :style="popupStyle.triangle" />
</div> </div>
<keypress <keypress
:disable="!visible" :disable="!visible"
@esc="_close(-1)" @esc="_close(-1)"
/> />
</uni-actionsheet> </uni-actionsheet>
</template> </template>
<script> <script>
import popup from './mixins/popup' import popup from './mixins/popup'
import keypress from '../../../helpers/keypress' import keypress from '../../../helpers/keypress'
import i18n from 'uni-mixins/i18n' import {
i18nMixin
} from 'uni-core/helpers/i18n'
import touchtrack from 'uni-mixins/touchtrack' import touchtrack from 'uni-mixins/touchtrack'
import scroller from 'uni-mixins/scroller/index' import scroller from 'uni-mixins/scroller/index'
import { import {
...@@ -110,8 +108,10 @@ function initClick (dom) { ...@@ -110,8 +108,10 @@ function initClick (dom) {
export default { export default {
name: 'ActionSheet', name: 'ActionSheet',
components: { keypress }, components: {
mixins: [i18n, popup, touchtrack, scroller], keypress
},
mixins: [i18nMixin, popup, touchtrack, scroller],
props: { props: {
title: { title: {
type: String, type: String,
...@@ -150,7 +150,9 @@ export default { ...@@ -150,7 +150,9 @@ export default {
if (newValue) { if (newValue) {
this.$nextTick(() => { this.$nextTick(() => {
// title 占位 // title 占位
if (this.title) { this.titleHeight = document.querySelector('.uni-actionsheet__title').offsetHeight } if (this.title) {
this.titleHeight = document.querySelector('.uni-actionsheet__title').offsetHeight
}
// 滚动条更新 // 滚动条更新
this._scroller.update() this._scroller.update()
// 获取contentHeight 滚动时使用 // 获取contentHeight 滚动时使用
...@@ -222,54 +224,54 @@ export default { ...@@ -222,54 +224,54 @@ export default {
} }
</script> </script>
<style> <style>
uni-actionsheet { uni-actionsheet {
display: block; display: block;
box-sizing: border-box; box-sizing: border-box;
} }
uni-actionsheet .uni-actionsheet { uni-actionsheet .uni-actionsheet {
position: fixed; position: fixed;
left: 6px; left: 6px;
right: 6px; right: 6px;
bottom: 6px; bottom: 6px;
transform: translate(0, 100%); transform: translate(0, 100%);
backface-visibility: hidden; backface-visibility: hidden;
z-index: 999; z-index: 999;
visibility: hidden; visibility: hidden;
transition: transform 0.3s, visibility 0.3s; transition: transform 0.3s, visibility 0.3s;
} }
uni-actionsheet .uni-actionsheet.uni-actionsheet_toggle { uni-actionsheet .uni-actionsheet.uni-actionsheet_toggle {
visibility: visible; visibility: visible;
transform: translate(0, 0); transform: translate(0, 0);
} }
uni-actionsheet .uni-actionsheet * { uni-actionsheet .uni-actionsheet * {
box-sizing: border-box; box-sizing: border-box;
} }
uni-actionsheet .uni-actionsheet__menu, uni-actionsheet .uni-actionsheet__menu,
uni-actionsheet .uni-actionsheet__action { uni-actionsheet .uni-actionsheet__action {
border-radius: 5px; border-radius: 5px;
background-color: #fcfcfd; background-color: #fcfcfd;
} }
uni-actionsheet .uni-actionsheet__action { uni-actionsheet .uni-actionsheet__action {
margin-top: 6px; margin-top: 6px;
} }
uni-actionsheet .uni-actionsheet__cell, uni-actionsheet .uni-actionsheet__cell,
uni-actionsheet .uni-actionsheet__title { uni-actionsheet .uni-actionsheet__title {
position: relative; position: relative;
padding: 10px 6px; padding: 10px 6px;
text-align: center; text-align: center;
font-size: 18px; font-size: 18px;
text-overflow: ellipsis; text-overflow: ellipsis;
overflow: hidden; overflow: hidden;
cursor: pointer; cursor: pointer;
} }
uni-actionsheet .uni-actionsheet__title{ uni-actionsheet .uni-actionsheet__title {
position: absolute; position: absolute;
top: 0; top: 0;
right: 0; right: 0;
...@@ -278,52 +280,56 @@ uni-actionsheet .uni-actionsheet__title{ ...@@ -278,52 +280,56 @@ uni-actionsheet .uni-actionsheet__title{
background-color: #fff; background-color: #fff;
border-radius: 5px 5px 0 0; border-radius: 5px 5px 0 0;
border-bottom: 1px solid #e5e5e5; border-bottom: 1px solid #e5e5e5;
}
uni-actionsheet .uni-actionsheet__cell:before {
content: " ";
position: absolute;
left: 0;
top: 0;
right: 0;
height: 1px;
border-top: 1px solid #e5e5e5;
color: #e5e5e5;
transform-origin: 0 0;
transform: scaleY(0.5);
}
uni-actionsheet .uni-actionsheet__cell:active {
background-color: #ececec;
}
uni-actionsheet .uni-actionsheet__cell:first-child:before {
display: none;
}
@media screen and (min-width: 500px) and (min-height: 500px) {
.uni-mask.uni-actionsheet__mask {
background: none;
} }
uni-actionsheet .uni-actionsheet {
width: 300px; uni-actionsheet .uni-actionsheet__cell:before {
left: 50%; content: " ";
right: auto; position: absolute;
top: 50%; left: 0;
bottom: auto; top: 0;
transform: translate(-50%, -50%); right: 0;
opacity: 0; height: 1px;
transition: opacity 0.3s, visibility 0.3s; border-top: 1px solid #e5e5e5;
} color: #e5e5e5;
uni-actionsheet .uni-actionsheet.uni-actionsheet_toggle { transform-origin: 0 0;
opacity: 1; transform: scaleY(0.5);
transform: translate(-50%, -50%);
} }
uni-actionsheet .uni-actionsheet__menu {
box-shadow: 0px 0 20px 5px rgba(0, 0, 0, 0.3); uni-actionsheet .uni-actionsheet__cell:active {
background-color: #ececec;
} }
uni-actionsheet .uni-actionsheet__action {
uni-actionsheet .uni-actionsheet__cell:first-child:before {
display: none; display: none;
} }
}
</style> @media screen and (min-width: 500px) and (min-height: 500px) {
.uni-mask.uni-actionsheet__mask {
background: none;
}
uni-actionsheet .uni-actionsheet {
width: 300px;
left: 50%;
right: auto;
top: 50%;
bottom: auto;
transform: translate(-50%, -50%);
opacity: 0;
transition: opacity 0.3s, visibility 0.3s;
}
uni-actionsheet .uni-actionsheet.uni-actionsheet_toggle {
opacity: 1;
transform: translate(-50%, -50%);
}
uni-actionsheet .uni-actionsheet__menu {
box-shadow: 0px 0 20px 5px rgba(0, 0, 0, 0.3);
}
uni-actionsheet .uni-actionsheet__action {
display: none;
}
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册