JavaScript

[JavaScript]スクロール位置を保存してリロード

    /**
     * initWithScrollY
     *
     * @static
     */
    static initWithScrollY(){
        let scrollY = localStorage.getItem('scrollKey');
        if(!scrollY){
            return;
        }
        localStorage.removeItem('scrollKey');
        scrollY = Number(scrollY);
        window.scrollTo(0, scrollY);
    }
    /**
     * ローカルストレージ取得
     *
     * @static
     * @param {String} key
     * @param {Boolean} remove
     * @returns {*}
     */
    static getLS(key,remove){
        const value = localStorage.getItem(key);
        if(remove){
            localStorage.removeItem(key);
        }
        return value;
    }
    /**
     * スクロール位置を保存しつつリロード
     *
     * @static
     * @param {*} delay
     */
    static reloadWithScrollY(delay){
        if(delay){
            setTimeout(()=>{
                Utils.reloadWithScrollY();
            },delay);
        }else{
            localStorage.setItem('scrollKey',window.scrollY);
            location.reload();
        }
    }
    /**
     * ローカルストレージ保存
     *
     * @static
     */
    static setLS(key,value){
        localStorage.setItem(key,value);
    }
    /**
     * スクロール位置を保存
     *
     * @static
     */
    static setScrollYToLS(){
        localStorage.setItem('scrollKey',window.scrollY);
    }

[JavaScript]useAppMetafields

let j,productId;
const cartLines = useCartLines();
const appMetafields = useAppMetafields();
if(appMetafields.length){
  cartLines.forEach((cartLine,i)=>{
    productId = cartLine.merchandise.product.id.split("/").pop();
    for(j=0;j<appMetafields.length;j++){
      if(appMetafields[j].target.id === productId){
        if(appMetafields[j].metafield.key === "metafieldKey" && appMetafields[j].metafield.value === "metafieldValue"){
          //処理
        }
        break;
      }
    }
  });
}

[JavaScript]オプショナルチェーン備忘録

1

const tmp = {name:"abc"};
console.log(tmp.address?.length);
undefined

 
 

2

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length);
NaN

 
 

3

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length > 4);
false

 
 

4

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length == 0);
false

 
 

5

const tmp = {name:""};
console.log(tmp.name?.length+tmp.city?.length <= 1);
false

 
 

6

const tmp = {name:""};
console.log(tmp.name?.length <= 1);
true

 
 

[JavaScript]ブラウザバック判定

isBrowserBack(){
    const perfEntries = performance.getEntriesByType("navigation");
    let result = false;
    perfEntries.forEach((perfEntry) => {
        if(perfEntry.type == 'back_forward'){
            result = true;
        }
    });
    return result;
}

[JavaScript]Promise

getCart(){
    return new Promise((resolve,reject) => {
        const url = 'url';
        fetch(url)
            .then((response)=>{
                if (!response.ok) {
                    throw new Error();
                }
                return response.json();
            })
            .then((json)=>{
                resolve(json);
            })
            .catch((error)=>{
                reject(error);
            });
    });
}

this.getCart()
.then((json)=>{
    
})
.catch((error)=>{

});

[JavaScript]文字列から漢字抽出

文字列から漢字抽出

const str = 'ひらがな小林カタカナ漢字';
const pattern = /(\p{scx=Han}+)/ug;
const matches = str.match(pattern);
console.log(matches);

出力

(2) ['小林', '漢字']

[Shopify][React]カスタムアプリでViewを表示する

import {
    View,
    Text,
    useSettings,
    reactExtension,
} from '@shopify/ui-extensions-react/checkout';

export default reactExtension("purchase.checkout.block.render", () => (
    <Extension />
));

function Extension() {
    const { title,left,border } = useSettings();
    const _title = title ? title : 'タイトル';
    const _left = left ? "start" : "center"
    const _border = border ? "none" : "base"
    return (
        <View display="block" padding="base" border={_border} cornerRadius="base" inlineAlignment={_left}>
            <Text size="base" emphasis="bold">{_title}</Text>
        </View>
    );
}

 
shopify.extension.tomlに追記

[extensions.settings]
[[extensions.settings.fields]]
key = "title"
type = "single_line_text_field"
name = "表示する文言"
description = "表示する文言を入力してください"
[[extensions.settings.fields]]
key = "left"
type = "boolean"
name = "テキストを左寄せにする"
[[extensions.settings.fields]]
key = "border"
type = "boolean"
name = "枠線を表示しない"