polotno-project/polotno-studio

Can new pages be added with some pre-filled attributes?

Closed this issue · 3 comments

ADTC commented

Currently all new pages added by clicking the + sign in the timeline / page thumbnails or by clicking the + button in the canvas (above each page), they are always with default attributes.

I was hoping to pre-fill any newly created pages with a custom set of attributes (the same argument typically passed to page.set(attr)). Would this be possible by any chance?

The programmatic solution can be this:

let lastPagesIds = [];
store.on('change', () => {
  const newPages = store.pages.filter(page => !lastPagesIds.includes(page.id));
  
  newPages.forEach(page => {
    page.set({});
  })

  // Update the lastPagesIds to reflect the current state
  lastPagesIds = store.pages.map(page => page.id);
});
ADTC commented

Almost, except it runs on duplicated pages as well. Since I'm trying to set a custom attribute, I figured I can just check for the attribute.

  newPages.forEach((page) => {
    // Duplicated pages will already have the custom attribute
    if (page.custom?.myAttribute !== undefined) return

    page.set({
      custom: {
        myAttribute: 'My attribute value',
      },
    })
  })

Thank you for you help! Though It would have been nice if we could just set the default attributes in createStore.

Good suggestion. Added to the backlog: polotno-project/polotno-board#81