174174 <new-block-selector
175175 :record-page =" !!module"
176176 :existing-layout-blocks =" selectableExistingLayoutBlocks"
177- :selectable-namespace- global-blocks =" selectableNamespaceGlobalBlocks"
177+ :selectable-global-blocks =" selectableNamespaceGlobalBlocks"
178178 style =" max-height : 75vh ;"
179179 @select =" addBlock"
180180 />
@@ -443,6 +443,7 @@ export default {
443443 getModuleByID: ' module/getByID' ,
444444 previousPage: ' ui/previousPage' ,
445445 namespaces: ' namespace/set' ,
446+ getNamespaceBlocks: ' namespace/getNamespaceBlocksByID' ,
446447 }),
447448
448449 trPage: {
@@ -508,19 +509,9 @@ export default {
508509
509510 selectableNamespaceGlobalBlocks () {
510511 const { namespaceID } = this .namespace
511- const namespace = this .namespaces .find ((n ) => n .namespaceID === namespaceID)
512512
513- if (! namespace) {
514- return []
515- }
516-
517- return cloneDeep (namespace .blocks ).map ((b , i ) => {
518- const block = compose .PageBlockMaker (b)
519- block .blockID = ` ${ namespaceID} -${ b .blockID } `
520-
521- return block
522- }).filter (b => {
523- return ! this .blocks .find (({ blockID }) => blockID === b .blockID )
513+ return this .getNamespaceBlocks (namespaceID).filter (({ blockID }) => {
514+ return ! this .usedBlocks .some (b => b .blockID === blockID)
524515 })
525516 },
526517
@@ -906,7 +897,7 @@ export default {
906897 this .findPageByID ({ ... this .page , force: true }),
907898 this .findLayoutByID ({ ... this .layout }),
908899 ]).then (async ([page , layout ]) => {
909- let blocks = [
900+ const blocks = [
910901 ... page .blocks .filter (({ blockID }) => {
911902 // Check if block exists in any other layout, if not delete it permanently
912903 return ! this .blocks .some (b => b .blockID === blockID) && this .layouts .some (({ pageLayoutID, blocks }) => pageLayoutID !== layout .pageLayoutID && blocks .some (b => b .blockID === blockID))
@@ -921,14 +912,16 @@ export default {
921912 b .meta .namespaceID ? namespaceBlocks .push (b) : pageBlocks .push (b)
922913 })
923914
915+ console .log (' namespaceBlocks' , namespaceBlocks)
916+
924917 const updatedGlobalBlocks = await this .processGlobalBlocks (namespaceBlocks)
925918
926919 return this .updatePage ({ namespaceID, ... page, blocks: pageBlocks })
927920 .then (this .updateTabbedBlockIDs )
928921 .then (async page => {
929922 const blocks = this .blocks .map (({ blockID, meta, xywh }) => {
930923 // If the global blocks is a newly created global block
931- if (meta .namespaceID && ! blockID . includes ( ' - ' ) ) {
924+ if (meta .namespaceID ) {
932925 blockID = (updatedGlobalBlocks .find (block => block .meta .tempID === meta .tempID ) || {}).blockID
933926 } else if (blockID === NoID) {
934927 blockID = (page .blocks .find (block => block .meta .tempID === meta .tempID ) || {}).blockID
@@ -970,24 +963,21 @@ export default {
970963
971964 let { namespaceID, name, slug, enabled, meta } = this .namespace
972965
973- // If new blocks are added to global blocks update the ID's
966+ // Block ID is already reset
974967 const newGlobalBlocks = globalBlocks
975- // We can't detect new global block through NOID because there can be an existing local block that
976- // was now made global previously and make a global block which means it does have the NoID added to it
977- .filter (({ blockID }) => ! blockID .includes (' -' ))
968+ .filter (({ blockID }) => ! blockID .includes (namespaceID))
978969 .map ((block ) => {
979970 block .meta .namespaceID = namespaceID
980971
981972 return block
982973 })
983974
984- // Update existing global blocks
985- const existingGlobalBlocks = this .namespace .blocks
975+ const namespace = this .namespaces .find ((n ) => n .namespaceID === this .namespace .namespaceID )
976+
977+ const existingGlobalBlocks = namespace .blocks
986978
987- // Update the state of the existing global blocks so the include the updated state changes
988979 globalBlocks .forEach ((block ) => {
989- block .blockID = String (block .blockID ).replace (` ${ namespaceID} -` , ' ' )
990- const matchingBlockIndex = existingGlobalBlocks .findIndex (({ blockID }) => blockID === block .blockID )
980+ const matchingBlockIndex = existingGlobalBlocks .findIndex (({ meta }) => meta .tempID === block .meta .tempID )
991981
992982 if (matchingBlockIndex > - 1 ) {
993983 const normalBlockID = existingGlobalBlocks[matchingBlockIndex].blockID
@@ -1187,9 +1177,9 @@ export default {
11871177 meta,
11881178 }))
11891179
1190- block .blockID = meta .namespaceID ? ` ${ namespaceID} -${ block .blockID } ` : block .blockID
1191-
11921180 if (block) {
1181+ block .blockID = meta .namespaceID ? ` ${ namespaceID} -${ block .blockID } ` : block .blockID
1182+
11931183 block .xywh = xywh
11941184 block .meta .hidden = !! meta .hidden
11951185 tempBlocks .push (block)
@@ -1255,11 +1245,9 @@ export default {
12551245 blockID = fetchID ({ blockID, meta })
12561246
12571247 if (meta .namespaceID ) {
1258- const namespace = this .namespaces .find ((n ) => n .namespaceID === this .namespace .namespaceID )
1248+ const { blocks = [] } = this .namespaces .find ((n ) => n .namespaceID === this .namespace .namespaceID ) || {}
12591249
1260- if (namespace) {
1261- return namespace .blocks .find ((b ) => fetchID (b) === blockID)
1262- }
1250+ return blocks .find ((b ) => fetchID (b) === blockID)
12631251 }
12641252
12651253 return this .page .blocks .find ((b ) => fetchID (b) === blockID)
@@ -1293,4 +1281,4 @@ div.toolbox {
12931281 right : auto ;
12941282 }
12951283}
1296- </style >
1284+ </style >, includes
0 commit comments